]>
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. | |
8 | */ | |
9 | ||
10 | #include "block.h" | |
11 | #include "blockdev.h" | |
12 | #include "monitor.h" | |
13 | #include "qerror.h" | |
14 | #include "qemu-option.h" | |
15 | #include "qemu-config.h" | |
16 | #include "sysemu.h" | |
9063f814 | 17 | #include "block_int.h" |
a4dea8a9 | 18 | #include "qmp-commands.h" |
666daa68 | 19 | |
c9b62a7e | 20 | static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); |
666daa68 | 21 | |
1960966d MA |
22 | static const char *const if_name[IF_COUNT] = { |
23 | [IF_NONE] = "none", | |
24 | [IF_IDE] = "ide", | |
25 | [IF_SCSI] = "scsi", | |
26 | [IF_FLOPPY] = "floppy", | |
27 | [IF_PFLASH] = "pflash", | |
28 | [IF_MTD] = "mtd", | |
29 | [IF_SD] = "sd", | |
30 | [IF_VIRTIO] = "virtio", | |
31 | [IF_XEN] = "xen", | |
32 | }; | |
33 | ||
34 | static const int if_max_devs[IF_COUNT] = { | |
27d6bf40 MA |
35 | /* |
36 | * Do not change these numbers! They govern how drive option | |
37 | * index maps to unit and bus. That mapping is ABI. | |
38 | * | |
39 | * All controllers used to imlement if=T drives need to support | |
40 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. | |
41 | * Otherwise, some index values map to "impossible" bus, unit | |
42 | * values. | |
43 | * | |
44 | * For instance, if you change [IF_SCSI] to 255, -drive | |
45 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
46 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
47 | * the drive can't be set up. Regression. | |
48 | */ | |
49 | [IF_IDE] = 2, | |
50 | [IF_SCSI] = 7, | |
1960966d MA |
51 | }; |
52 | ||
14bafc54 MA |
53 | /* |
54 | * We automatically delete the drive when a device using it gets | |
55 | * unplugged. Questionable feature, but we can't just drop it. | |
56 | * Device models call blockdev_mark_auto_del() to schedule the | |
57 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
58 | * when deletion is actually safe. | |
59 | */ | |
60 | void blockdev_mark_auto_del(BlockDriverState *bs) | |
61 | { | |
62 | DriveInfo *dinfo = drive_get_by_blockdev(bs); | |
63 | ||
0fc0f1fa RH |
64 | if (dinfo) { |
65 | dinfo->auto_del = 1; | |
66 | } | |
14bafc54 MA |
67 | } |
68 | ||
69 | void blockdev_auto_del(BlockDriverState *bs) | |
70 | { | |
71 | DriveInfo *dinfo = drive_get_by_blockdev(bs); | |
72 | ||
0fc0f1fa | 73 | if (dinfo && dinfo->auto_del) { |
84fb3925 | 74 | drive_put_ref(dinfo); |
14bafc54 MA |
75 | } |
76 | } | |
77 | ||
505a7fb1 MA |
78 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
79 | { | |
80 | int max_devs = if_max_devs[type]; | |
81 | return max_devs ? index / max_devs : 0; | |
82 | } | |
83 | ||
84 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
85 | { | |
86 | int max_devs = if_max_devs[type]; | |
87 | return max_devs ? index % max_devs : index; | |
88 | } | |
89 | ||
2292ddae MA |
90 | QemuOpts *drive_def(const char *optstr) |
91 | { | |
92 | return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); | |
93 | } | |
94 | ||
95 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 96 | const char *optstr) |
666daa68 | 97 | { |
666daa68 | 98 | QemuOpts *opts; |
2292ddae | 99 | char buf[32]; |
666daa68 | 100 | |
2292ddae | 101 | opts = drive_def(optstr); |
666daa68 MA |
102 | if (!opts) { |
103 | return NULL; | |
104 | } | |
2292ddae MA |
105 | if (type != IF_DEFAULT) { |
106 | qemu_opt_set(opts, "if", if_name[type]); | |
107 | } | |
108 | if (index >= 0) { | |
109 | snprintf(buf, sizeof(buf), "%d", index); | |
110 | qemu_opt_set(opts, "index", buf); | |
111 | } | |
666daa68 MA |
112 | if (file) |
113 | qemu_opt_set(opts, "file", file); | |
114 | return opts; | |
115 | } | |
116 | ||
117 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
118 | { | |
119 | DriveInfo *dinfo; | |
120 | ||
121 | /* seek interface, bus and unit */ | |
122 | ||
123 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
124 | if (dinfo->type == type && | |
125 | dinfo->bus == bus && | |
126 | dinfo->unit == unit) | |
127 | return dinfo; | |
128 | } | |
129 | ||
130 | return NULL; | |
131 | } | |
132 | ||
f1bd51ac MA |
133 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
134 | { | |
135 | return drive_get(type, | |
136 | drive_index_to_bus_id(type, index), | |
137 | drive_index_to_unit_id(type, index)); | |
138 | } | |
139 | ||
666daa68 MA |
140 | int drive_get_max_bus(BlockInterfaceType type) |
141 | { | |
142 | int max_bus; | |
143 | DriveInfo *dinfo; | |
144 | ||
145 | max_bus = -1; | |
146 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
147 | if(dinfo->type == type && | |
148 | dinfo->bus > max_bus) | |
149 | max_bus = dinfo->bus; | |
150 | } | |
151 | return max_bus; | |
152 | } | |
153 | ||
13839974 MA |
154 | /* Get a block device. This should only be used for single-drive devices |
155 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
156 | appropriate bus. */ | |
157 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
158 | { | |
159 | static int next_block_unit[IF_COUNT]; | |
160 | ||
161 | return drive_get(type, 0, next_block_unit[type]++); | |
162 | } | |
163 | ||
e4700e59 | 164 | DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) |
666daa68 MA |
165 | { |
166 | DriveInfo *dinfo; | |
167 | ||
168 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
e4700e59 MA |
169 | if (dinfo->bdrv == bs) { |
170 | return dinfo; | |
171 | } | |
666daa68 | 172 | } |
e4700e59 | 173 | return NULL; |
666daa68 MA |
174 | } |
175 | ||
666daa68 MA |
176 | static void bdrv_format_print(void *opaque, const char *name) |
177 | { | |
807105a7 | 178 | error_printf(" %s", name); |
666daa68 MA |
179 | } |
180 | ||
84fb3925 | 181 | static void drive_uninit(DriveInfo *dinfo) |
666daa68 MA |
182 | { |
183 | qemu_opts_del(dinfo->opts); | |
184 | bdrv_delete(dinfo->bdrv); | |
7267c094 | 185 | g_free(dinfo->id); |
666daa68 | 186 | QTAILQ_REMOVE(&drives, dinfo, next); |
7267c094 | 187 | g_free(dinfo); |
666daa68 MA |
188 | } |
189 | ||
84fb3925 MT |
190 | void drive_put_ref(DriveInfo *dinfo) |
191 | { | |
192 | assert(dinfo->refcount); | |
193 | if (--dinfo->refcount == 0) { | |
194 | drive_uninit(dinfo); | |
195 | } | |
196 | } | |
197 | ||
198 | void drive_get_ref(DriveInfo *dinfo) | |
199 | { | |
200 | dinfo->refcount++; | |
201 | } | |
202 | ||
666daa68 MA |
203 | static int parse_block_error_action(const char *buf, int is_read) |
204 | { | |
205 | if (!strcmp(buf, "ignore")) { | |
206 | return BLOCK_ERR_IGNORE; | |
207 | } else if (!is_read && !strcmp(buf, "enospc")) { | |
208 | return BLOCK_ERR_STOP_ENOSPC; | |
209 | } else if (!strcmp(buf, "stop")) { | |
210 | return BLOCK_ERR_STOP_ANY; | |
211 | } else if (!strcmp(buf, "report")) { | |
212 | return BLOCK_ERR_REPORT; | |
213 | } else { | |
807105a7 MA |
214 | error_report("'%s' invalid %s error action", |
215 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
216 | return -1; |
217 | } | |
218 | } | |
219 | ||
0563e191 ZYW |
220 | static bool do_check_io_limits(BlockIOLimit *io_limits) |
221 | { | |
222 | bool bps_flag; | |
223 | bool iops_flag; | |
224 | ||
225 | assert(io_limits); | |
226 | ||
227 | bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0) | |
228 | && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0) | |
229 | || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0)); | |
230 | iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0) | |
231 | && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0) | |
232 | || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0)); | |
233 | if (bps_flag || iops_flag) { | |
234 | return false; | |
235 | } | |
236 | ||
237 | return true; | |
238 | } | |
239 | ||
319ae529 | 240 | DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi) |
666daa68 MA |
241 | { |
242 | const char *buf; | |
243 | const char *file = NULL; | |
244 | char devname[128]; | |
245 | const char *serial; | |
246 | const char *mediastr = ""; | |
247 | BlockInterfaceType type; | |
248 | enum { MEDIA_DISK, MEDIA_CDROM } media; | |
249 | int bus_id, unit_id; | |
250 | int cyls, heads, secs, translation; | |
251 | BlockDriver *drv = NULL; | |
252 | int max_devs; | |
253 | int index; | |
254 | int ro = 0; | |
255 | int bdrv_flags = 0; | |
256 | int on_read_error, on_write_error; | |
257 | const char *devaddr; | |
258 | DriveInfo *dinfo; | |
0563e191 | 259 | BlockIOLimit io_limits; |
666daa68 | 260 | int snapshot = 0; |
fb0490f6 | 261 | bool copy_on_read; |
666daa68 MA |
262 | int ret; |
263 | ||
666daa68 | 264 | translation = BIOS_ATA_TRANSLATION_AUTO; |
666daa68 MA |
265 | media = MEDIA_DISK; |
266 | ||
267 | /* extract parameters */ | |
268 | bus_id = qemu_opt_get_number(opts, "bus", 0); | |
269 | unit_id = qemu_opt_get_number(opts, "unit", -1); | |
270 | index = qemu_opt_get_number(opts, "index", -1); | |
271 | ||
272 | cyls = qemu_opt_get_number(opts, "cyls", 0); | |
273 | heads = qemu_opt_get_number(opts, "heads", 0); | |
274 | secs = qemu_opt_get_number(opts, "secs", 0); | |
275 | ||
276 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); | |
277 | ro = qemu_opt_get_bool(opts, "readonly", 0); | |
fb0490f6 | 278 | copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); |
666daa68 MA |
279 | |
280 | file = qemu_opt_get(opts, "file"); | |
281 | serial = qemu_opt_get(opts, "serial"); | |
282 | ||
283 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { | |
284 | pstrcpy(devname, sizeof(devname), buf); | |
1960966d MA |
285 | for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++) |
286 | ; | |
287 | if (type == IF_COUNT) { | |
807105a7 | 288 | error_report("unsupported bus type '%s'", buf); |
666daa68 MA |
289 | return NULL; |
290 | } | |
2d3999fe LC |
291 | } else { |
292 | type = default_to_scsi ? IF_SCSI : IF_IDE; | |
293 | pstrcpy(devname, sizeof(devname), if_name[type]); | |
666daa68 | 294 | } |
2d3999fe | 295 | |
1960966d | 296 | max_devs = if_max_devs[type]; |
666daa68 MA |
297 | |
298 | if (cyls || heads || secs) { | |
299 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { | |
807105a7 | 300 | error_report("invalid physical cyls number"); |
666daa68 MA |
301 | return NULL; |
302 | } | |
303 | if (heads < 1 || (type == IF_IDE && heads > 16)) { | |
807105a7 | 304 | error_report("invalid physical heads number"); |
666daa68 MA |
305 | return NULL; |
306 | } | |
307 | if (secs < 1 || (type == IF_IDE && secs > 63)) { | |
807105a7 | 308 | error_report("invalid physical secs number"); |
666daa68 MA |
309 | return NULL; |
310 | } | |
311 | } | |
312 | ||
313 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { | |
314 | if (!cyls) { | |
e4080f9b | 315 | error_report("'%s' trans must be used with cyls, heads and secs", |
807105a7 | 316 | buf); |
666daa68 MA |
317 | return NULL; |
318 | } | |
319 | if (!strcmp(buf, "none")) | |
320 | translation = BIOS_ATA_TRANSLATION_NONE; | |
321 | else if (!strcmp(buf, "lba")) | |
322 | translation = BIOS_ATA_TRANSLATION_LBA; | |
323 | else if (!strcmp(buf, "auto")) | |
324 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
325 | else { | |
807105a7 | 326 | error_report("'%s' invalid translation type", buf); |
666daa68 MA |
327 | return NULL; |
328 | } | |
329 | } | |
330 | ||
331 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { | |
332 | if (!strcmp(buf, "disk")) { | |
333 | media = MEDIA_DISK; | |
334 | } else if (!strcmp(buf, "cdrom")) { | |
335 | if (cyls || secs || heads) { | |
e7ff8f0e | 336 | error_report("CHS can't be set with media=%s", buf); |
666daa68 MA |
337 | return NULL; |
338 | } | |
339 | media = MEDIA_CDROM; | |
340 | } else { | |
807105a7 | 341 | error_report("'%s' invalid media", buf); |
666daa68 MA |
342 | return NULL; |
343 | } | |
344 | } | |
345 | ||
346 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { | |
c3993cdc SH |
347 | if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) { |
348 | error_report("invalid cache option"); | |
349 | return NULL; | |
666daa68 MA |
350 | } |
351 | } | |
352 | ||
353 | #ifdef CONFIG_LINUX_AIO | |
354 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
355 | if (!strcmp(buf, "native")) { | |
356 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
357 | } else if (!strcmp(buf, "threads")) { | |
358 | /* this is the default */ | |
359 | } else { | |
807105a7 | 360 | error_report("invalid aio option"); |
666daa68 MA |
361 | return NULL; |
362 | } | |
363 | } | |
364 | #endif | |
365 | ||
366 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
367 | if (strcmp(buf, "?") == 0) { | |
807105a7 MA |
368 | error_printf("Supported formats:"); |
369 | bdrv_iterate_format(bdrv_format_print, NULL); | |
370 | error_printf("\n"); | |
371 | return NULL; | |
666daa68 MA |
372 | } |
373 | drv = bdrv_find_whitelisted_format(buf); | |
374 | if (!drv) { | |
807105a7 | 375 | error_report("'%s' invalid format", buf); |
666daa68 MA |
376 | return NULL; |
377 | } | |
378 | } | |
379 | ||
0563e191 ZYW |
380 | /* disk I/O throttling */ |
381 | io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = | |
382 | qemu_opt_get_number(opts, "bps", 0); | |
383 | io_limits.bps[BLOCK_IO_LIMIT_READ] = | |
384 | qemu_opt_get_number(opts, "bps_rd", 0); | |
385 | io_limits.bps[BLOCK_IO_LIMIT_WRITE] = | |
386 | qemu_opt_get_number(opts, "bps_wr", 0); | |
387 | io_limits.iops[BLOCK_IO_LIMIT_TOTAL] = | |
388 | qemu_opt_get_number(opts, "iops", 0); | |
389 | io_limits.iops[BLOCK_IO_LIMIT_READ] = | |
390 | qemu_opt_get_number(opts, "iops_rd", 0); | |
391 | io_limits.iops[BLOCK_IO_LIMIT_WRITE] = | |
392 | qemu_opt_get_number(opts, "iops_wr", 0); | |
393 | ||
394 | if (!do_check_io_limits(&io_limits)) { | |
395 | error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) " | |
396 | "cannot be used at the same time"); | |
397 | return NULL; | |
398 | } | |
399 | ||
666daa68 MA |
400 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
401 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { | |
402 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { | |
807105a7 | 403 | error_report("werror is not supported by this bus type"); |
666daa68 MA |
404 | return NULL; |
405 | } | |
406 | ||
407 | on_write_error = parse_block_error_action(buf, 0); | |
408 | if (on_write_error < 0) { | |
409 | return NULL; | |
410 | } | |
411 | } | |
412 | ||
413 | on_read_error = BLOCK_ERR_REPORT; | |
414 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { | |
5dba48a8 | 415 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { |
807105a7 | 416 | error_report("rerror is not supported by this bus type"); |
666daa68 MA |
417 | return NULL; |
418 | } | |
419 | ||
420 | on_read_error = parse_block_error_action(buf, 1); | |
421 | if (on_read_error < 0) { | |
422 | return NULL; | |
423 | } | |
424 | } | |
425 | ||
426 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { | |
427 | if (type != IF_VIRTIO) { | |
807105a7 | 428 | error_report("addr is not supported by this bus type"); |
666daa68 MA |
429 | return NULL; |
430 | } | |
431 | } | |
432 | ||
433 | /* compute bus and unit according index */ | |
434 | ||
435 | if (index != -1) { | |
436 | if (bus_id != 0 || unit_id != -1) { | |
807105a7 | 437 | error_report("index cannot be used with bus and unit"); |
666daa68 MA |
438 | return NULL; |
439 | } | |
505a7fb1 MA |
440 | bus_id = drive_index_to_bus_id(type, index); |
441 | unit_id = drive_index_to_unit_id(type, index); | |
666daa68 MA |
442 | } |
443 | ||
444 | /* if user doesn't specify a unit_id, | |
445 | * try to find the first free | |
446 | */ | |
447 | ||
448 | if (unit_id == -1) { | |
449 | unit_id = 0; | |
450 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
451 | unit_id++; | |
452 | if (max_devs && unit_id >= max_devs) { | |
453 | unit_id -= max_devs; | |
454 | bus_id++; | |
455 | } | |
456 | } | |
457 | } | |
458 | ||
459 | /* check unit id */ | |
460 | ||
461 | if (max_devs && unit_id >= max_devs) { | |
807105a7 MA |
462 | error_report("unit %d too big (max is %d)", |
463 | unit_id, max_devs - 1); | |
666daa68 MA |
464 | return NULL; |
465 | } | |
466 | ||
467 | /* | |
4e5d9b57 | 468 | * catch multiple definitions |
666daa68 MA |
469 | */ |
470 | ||
471 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
4e5d9b57 MA |
472 | error_report("drive with bus=%d, unit=%d (index=%d) exists", |
473 | bus_id, unit_id, index); | |
666daa68 MA |
474 | return NULL; |
475 | } | |
476 | ||
477 | /* init */ | |
478 | ||
7267c094 | 479 | dinfo = g_malloc0(sizeof(*dinfo)); |
666daa68 | 480 | if ((buf = qemu_opts_id(opts)) != NULL) { |
7267c094 | 481 | dinfo->id = g_strdup(buf); |
666daa68 MA |
482 | } else { |
483 | /* no id supplied -> create one */ | |
7267c094 | 484 | dinfo->id = g_malloc0(32); |
666daa68 MA |
485 | if (type == IF_IDE || type == IF_SCSI) |
486 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
487 | if (max_devs) | |
488 | snprintf(dinfo->id, 32, "%s%i%s%i", | |
489 | devname, bus_id, mediastr, unit_id); | |
490 | else | |
491 | snprintf(dinfo->id, 32, "%s%s%i", | |
492 | devname, mediastr, unit_id); | |
493 | } | |
494 | dinfo->bdrv = bdrv_new(dinfo->id); | |
495 | dinfo->devaddr = devaddr; | |
496 | dinfo->type = type; | |
497 | dinfo->bus = bus_id; | |
498 | dinfo->unit = unit_id; | |
666daa68 | 499 | dinfo->opts = opts; |
84fb3925 | 500 | dinfo->refcount = 1; |
666daa68 | 501 | if (serial) |
653dbec7 | 502 | strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1); |
666daa68 MA |
503 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
504 | ||
abd7f68d MA |
505 | bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
506 | ||
0563e191 ZYW |
507 | /* disk I/O throttling */ |
508 | bdrv_set_io_limits(dinfo->bdrv, &io_limits); | |
509 | ||
666daa68 MA |
510 | switch(type) { |
511 | case IF_IDE: | |
512 | case IF_SCSI: | |
513 | case IF_XEN: | |
514 | case IF_NONE: | |
515 | switch(media) { | |
516 | case MEDIA_DISK: | |
517 | if (cyls != 0) { | |
518 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); | |
519 | bdrv_set_translation_hint(dinfo->bdrv, translation); | |
520 | } | |
521 | break; | |
522 | case MEDIA_CDROM: | |
95b5edcd | 523 | dinfo->media_cd = 1; |
666daa68 MA |
524 | break; |
525 | } | |
526 | break; | |
527 | case IF_SD: | |
666daa68 | 528 | case IF_FLOPPY: |
666daa68 MA |
529 | case IF_PFLASH: |
530 | case IF_MTD: | |
531 | break; | |
532 | case IF_VIRTIO: | |
533 | /* add virtio block device */ | |
3329f07b | 534 | opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0); |
29f82b37 | 535 | qemu_opt_set(opts, "driver", "virtio-blk"); |
666daa68 MA |
536 | qemu_opt_set(opts, "drive", dinfo->id); |
537 | if (devaddr) | |
538 | qemu_opt_set(opts, "addr", devaddr); | |
539 | break; | |
2292ddae | 540 | default: |
666daa68 MA |
541 | abort(); |
542 | } | |
dd5b0d71 | 543 | if (!file || !*file) { |
319ae529 | 544 | return dinfo; |
666daa68 MA |
545 | } |
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 | ||
fb0490f6 SH |
552 | if (copy_on_read) { |
553 | bdrv_flags |= BDRV_O_COPY_ON_READ; | |
554 | } | |
555 | ||
666daa68 MA |
556 | if (media == MEDIA_CDROM) { |
557 | /* CDROM is fine for any interface, don't check. */ | |
558 | ro = 1; | |
559 | } else if (ro == 1) { | |
560 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) { | |
807105a7 | 561 | error_report("readonly not supported by this bus type"); |
a9ae2bff | 562 | goto err; |
666daa68 MA |
563 | } |
564 | } | |
565 | ||
566 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; | |
567 | ||
568 | ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv); | |
569 | if (ret < 0) { | |
807105a7 MA |
570 | error_report("could not open disk image %s: %s", |
571 | file, strerror(-ret)); | |
a9ae2bff | 572 | goto err; |
666daa68 MA |
573 | } |
574 | ||
575 | if (bdrv_key_required(dinfo->bdrv)) | |
576 | autostart = 0; | |
666daa68 | 577 | return dinfo; |
a9ae2bff MA |
578 | |
579 | err: | |
580 | bdrv_delete(dinfo->bdrv); | |
7267c094 | 581 | g_free(dinfo->id); |
a9ae2bff | 582 | QTAILQ_REMOVE(&drives, dinfo, next); |
7267c094 | 583 | g_free(dinfo); |
a9ae2bff | 584 | return NULL; |
666daa68 MA |
585 | } |
586 | ||
587 | void do_commit(Monitor *mon, const QDict *qdict) | |
588 | { | |
666daa68 | 589 | const char *device = qdict_get_str(qdict, "device"); |
6ab4b5ab | 590 | BlockDriverState *bs; |
666daa68 | 591 | |
6ab4b5ab MA |
592 | if (!strcmp(device, "all")) { |
593 | bdrv_commit_all(); | |
594 | } else { | |
595 | bs = bdrv_find(device); | |
ac59eb95 MA |
596 | if (!bs) { |
597 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
598 | return; | |
6ab4b5ab | 599 | } |
ac59eb95 | 600 | bdrv_commit(bs); |
666daa68 MA |
601 | } |
602 | } | |
603 | ||
6106e249 LC |
604 | void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, |
605 | bool has_format, const char *format, | |
606 | Error **errp) | |
f8882568 | 607 | { |
f8882568 | 608 | BlockDriverState *bs; |
52f9a172 | 609 | BlockDriver *drv, *old_drv, *proto_drv; |
f8882568 JS |
610 | int ret = 0; |
611 | int flags; | |
52f9a172 | 612 | char old_filename[1024]; |
f8882568 JS |
613 | |
614 | bs = bdrv_find(device); | |
615 | if (!bs) { | |
6106e249 LC |
616 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
617 | return; | |
f8882568 JS |
618 | } |
619 | ||
52f9a172 JS |
620 | pstrcpy(old_filename, sizeof(old_filename), bs->filename); |
621 | ||
622 | old_drv = bs->drv; | |
623 | flags = bs->open_flags; | |
624 | ||
6106e249 | 625 | if (!has_format) { |
f8882568 JS |
626 | format = "qcow2"; |
627 | } | |
628 | ||
629 | drv = bdrv_find_format(format); | |
630 | if (!drv) { | |
6106e249 LC |
631 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
632 | return; | |
f8882568 JS |
633 | } |
634 | ||
6106e249 | 635 | proto_drv = bdrv_find_protocol(snapshot_file); |
f8882568 | 636 | if (!proto_drv) { |
6106e249 LC |
637 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
638 | return; | |
f8882568 JS |
639 | } |
640 | ||
6106e249 | 641 | ret = bdrv_img_create(snapshot_file, format, bs->filename, |
52f9a172 | 642 | bs->drv->format_name, NULL, -1, flags); |
f8882568 | 643 | if (ret) { |
6106e249 LC |
644 | error_set(errp, QERR_UNDEFINED_ERROR); |
645 | return; | |
f8882568 JS |
646 | } |
647 | ||
922453bc | 648 | bdrv_drain_all(); |
f8882568 JS |
649 | bdrv_flush(bs); |
650 | ||
f8882568 | 651 | bdrv_close(bs); |
6106e249 | 652 | ret = bdrv_open(bs, snapshot_file, flags, drv); |
f8882568 | 653 | /* |
52f9a172 JS |
654 | * If reopening the image file we just created fails, fall back |
655 | * and try to re-open the original image. If that fails too, we | |
656 | * are in serious trouble. | |
f8882568 JS |
657 | */ |
658 | if (ret != 0) { | |
52f9a172 JS |
659 | ret = bdrv_open(bs, old_filename, flags, old_drv); |
660 | if (ret != 0) { | |
6106e249 | 661 | error_set(errp, QERR_OPEN_FILE_FAILED, old_filename); |
52f9a172 | 662 | } else { |
6106e249 | 663 | error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file); |
52f9a172 | 664 | } |
f8882568 | 665 | } |
f8882568 JS |
666 | } |
667 | ||
666daa68 MA |
668 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
669 | { | |
2c6942fa | 670 | if (!bdrv_dev_has_removable_media(bs)) { |
ea8f942f MA |
671 | qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs)); |
672 | return -1; | |
673 | } | |
025ccaa7 PB |
674 | if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) { |
675 | bdrv_dev_eject_request(bs, force); | |
676 | if (!force) { | |
677 | qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); | |
678 | return -1; | |
679 | } | |
666daa68 | 680 | } |
3b5276b5 | 681 | bdrv_close(bs); |
666daa68 MA |
682 | return 0; |
683 | } | |
684 | ||
685 | int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
686 | { | |
687 | BlockDriverState *bs; | |
eb159d13 | 688 | int force = qdict_get_try_bool(qdict, "force", 0); |
666daa68 MA |
689 | const char *filename = qdict_get_str(qdict, "device"); |
690 | ||
691 | bs = bdrv_find(filename); | |
692 | if (!bs) { | |
693 | qerror_report(QERR_DEVICE_NOT_FOUND, filename); | |
694 | return -1; | |
695 | } | |
696 | return eject_device(mon, bs, force); | |
697 | } | |
698 | ||
a4dea8a9 | 699 | void qmp_block_passwd(const char *device, const char *password, Error **errp) |
666daa68 MA |
700 | { |
701 | BlockDriverState *bs; | |
702 | int err; | |
703 | ||
a4dea8a9 | 704 | bs = bdrv_find(device); |
666daa68 | 705 | if (!bs) { |
a4dea8a9 LC |
706 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
707 | return; | |
666daa68 MA |
708 | } |
709 | ||
a4dea8a9 | 710 | err = bdrv_set_key(bs, password); |
666daa68 | 711 | if (err == -EINVAL) { |
a4dea8a9 LC |
712 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
713 | return; | |
666daa68 | 714 | } else if (err < 0) { |
a4dea8a9 LC |
715 | error_set(errp, QERR_INVALID_PASSWORD); |
716 | return; | |
666daa68 | 717 | } |
666daa68 MA |
718 | } |
719 | ||
720 | int do_change_block(Monitor *mon, const char *device, | |
721 | const char *filename, const char *fmt) | |
722 | { | |
723 | BlockDriverState *bs; | |
724 | BlockDriver *drv = NULL; | |
725 | int bdrv_flags; | |
726 | ||
727 | bs = bdrv_find(device); | |
728 | if (!bs) { | |
729 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
730 | return -1; | |
731 | } | |
732 | if (fmt) { | |
733 | drv = bdrv_find_whitelisted_format(fmt); | |
734 | if (!drv) { | |
735 | qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt); | |
736 | return -1; | |
737 | } | |
738 | } | |
739 | if (eject_device(mon, bs, 0) < 0) { | |
740 | return -1; | |
741 | } | |
528f7663 | 742 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
199630b6 | 743 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
666daa68 MA |
744 | if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { |
745 | qerror_report(QERR_OPEN_FILE_FAILED, filename); | |
746 | return -1; | |
747 | } | |
748 | return monitor_read_bdrv_key_start(mon, bs, NULL, NULL); | |
749 | } | |
9063f814 | 750 | |
727f005e ZYW |
751 | /* throttling disk I/O limits */ |
752 | int do_block_set_io_throttle(Monitor *mon, | |
753 | const QDict *qdict, QObject **ret_data) | |
754 | { | |
755 | BlockIOLimit io_limits; | |
756 | const char *devname = qdict_get_str(qdict, "device"); | |
757 | BlockDriverState *bs; | |
758 | ||
759 | io_limits.bps[BLOCK_IO_LIMIT_TOTAL] | |
760 | = qdict_get_try_int(qdict, "bps", -1); | |
761 | io_limits.bps[BLOCK_IO_LIMIT_READ] | |
762 | = qdict_get_try_int(qdict, "bps_rd", -1); | |
763 | io_limits.bps[BLOCK_IO_LIMIT_WRITE] | |
764 | = qdict_get_try_int(qdict, "bps_wr", -1); | |
765 | io_limits.iops[BLOCK_IO_LIMIT_TOTAL] | |
766 | = qdict_get_try_int(qdict, "iops", -1); | |
767 | io_limits.iops[BLOCK_IO_LIMIT_READ] | |
768 | = qdict_get_try_int(qdict, "iops_rd", -1); | |
769 | io_limits.iops[BLOCK_IO_LIMIT_WRITE] | |
770 | = qdict_get_try_int(qdict, "iops_wr", -1); | |
771 | ||
772 | bs = bdrv_find(devname); | |
773 | if (!bs) { | |
774 | qerror_report(QERR_DEVICE_NOT_FOUND, devname); | |
775 | return -1; | |
776 | } | |
777 | ||
778 | if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1) | |
779 | || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1) | |
780 | || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1) | |
781 | || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1) | |
782 | || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1) | |
783 | || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) { | |
784 | qerror_report(QERR_MISSING_PARAMETER, | |
785 | "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr"); | |
786 | return -1; | |
787 | } | |
788 | ||
789 | if (!do_check_io_limits(&io_limits)) { | |
790 | qerror_report(QERR_INVALID_PARAMETER_COMBINATION); | |
791 | return -1; | |
792 | } | |
793 | ||
794 | bs->io_limits = io_limits; | |
795 | bs->slice_time = BLOCK_IO_SLICE_TIME; | |
796 | ||
797 | if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) { | |
798 | bdrv_io_limits_enable(bs); | |
799 | } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) { | |
800 | bdrv_io_limits_disable(bs); | |
801 | } else { | |
802 | if (bs->block_timer) { | |
803 | qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock)); | |
804 | } | |
805 | } | |
806 | ||
807 | return 0; | |
808 | } | |
809 | ||
9063f814 RH |
810 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
811 | { | |
812 | const char *id = qdict_get_str(qdict, "id"); | |
813 | BlockDriverState *bs; | |
9063f814 RH |
814 | |
815 | bs = bdrv_find(id); | |
816 | if (!bs) { | |
817 | qerror_report(QERR_DEVICE_NOT_FOUND, id); | |
818 | return -1; | |
819 | } | |
8591675f MT |
820 | if (bdrv_in_use(bs)) { |
821 | qerror_report(QERR_DEVICE_IN_USE, id); | |
822 | return -1; | |
823 | } | |
9063f814 RH |
824 | |
825 | /* quiesce block driver; prevent further io */ | |
922453bc | 826 | bdrv_drain_all(); |
9063f814 RH |
827 | bdrv_flush(bs); |
828 | bdrv_close(bs); | |
829 | ||
fa879d62 | 830 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
831 | * then we need to make the drive anonymous until the device |
832 | * can be removed. If this is a drive with no device backing | |
833 | * then we can just get rid of the block driver state right here. | |
834 | */ | |
fa879d62 | 835 | if (bdrv_get_attached_dev(bs)) { |
d22b2f41 RH |
836 | bdrv_make_anon(bs); |
837 | } else { | |
838 | drive_uninit(drive_get_by_blockdev(bs)); | |
9063f814 RH |
839 | } |
840 | ||
9063f814 RH |
841 | return 0; |
842 | } | |
6d4a2b3a CH |
843 | |
844 | /* | |
845 | * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the | |
846 | * existing QERR_ macro mess is cleaned up. A good example for better | |
847 | * error reports can be found in the qemu-img resize code. | |
848 | */ | |
5e7caacb | 849 | void qmp_block_resize(const char *device, int64_t size, Error **errp) |
6d4a2b3a | 850 | { |
6d4a2b3a CH |
851 | BlockDriverState *bs; |
852 | ||
853 | bs = bdrv_find(device); | |
854 | if (!bs) { | |
5e7caacb LC |
855 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
856 | return; | |
6d4a2b3a CH |
857 | } |
858 | ||
859 | if (size < 0) { | |
5e7caacb LC |
860 | error_set(errp, QERR_UNDEFINED_ERROR); |
861 | return; | |
6d4a2b3a CH |
862 | } |
863 | ||
864 | if (bdrv_truncate(bs, size)) { | |
5e7caacb LC |
865 | error_set(errp, QERR_UNDEFINED_ERROR); |
866 | return; | |
6d4a2b3a | 867 | } |
6d4a2b3a | 868 | } |