]> Git Repo - qemu.git/blob - blockdev.c
block: move input parsing code in qmp_transaction()
[qemu.git] / blockdev.c
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  * 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.
31  */
32
33 #include "sysemu/blockdev.h"
34 #include "hw/block/block.h"
35 #include "block/blockjob.h"
36 #include "monitor/monitor.h"
37 #include "qapi/qmp/qerror.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "sysemu/sysemu.h"
42 #include "block/block_int.h"
43 #include "qmp-commands.h"
44 #include "trace.h"
45 #include "sysemu/arch_init.h"
46
47 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
48 extern QemuOptsList qemu_common_drive_opts;
49
50 static const char *const if_name[IF_COUNT] = {
51     [IF_NONE] = "none",
52     [IF_IDE] = "ide",
53     [IF_SCSI] = "scsi",
54     [IF_FLOPPY] = "floppy",
55     [IF_PFLASH] = "pflash",
56     [IF_MTD] = "mtd",
57     [IF_SD] = "sd",
58     [IF_VIRTIO] = "virtio",
59     [IF_XEN] = "xen",
60 };
61
62 static const int if_max_devs[IF_COUNT] = {
63     /*
64      * Do not change these numbers!  They govern how drive option
65      * index maps to unit and bus.  That mapping is ABI.
66      *
67      * All controllers used to imlement if=T drives need to support
68      * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
69      * Otherwise, some index values map to "impossible" bus, unit
70      * values.
71      *
72      * For instance, if you change [IF_SCSI] to 255, -drive
73      * if=scsi,index=12 no longer means bus=1,unit=5, but
74      * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
75      * the drive can't be set up.  Regression.
76      */
77     [IF_IDE] = 2,
78     [IF_SCSI] = 7,
79 };
80
81 /*
82  * We automatically delete the drive when a device using it gets
83  * unplugged.  Questionable feature, but we can't just drop it.
84  * Device models call blockdev_mark_auto_del() to schedule the
85  * automatic deletion, and generic qdev code calls blockdev_auto_del()
86  * when deletion is actually safe.
87  */
88 void blockdev_mark_auto_del(BlockDriverState *bs)
89 {
90     DriveInfo *dinfo = drive_get_by_blockdev(bs);
91
92     if (bs->job) {
93         block_job_cancel(bs->job);
94     }
95     if (dinfo) {
96         dinfo->auto_del = 1;
97     }
98 }
99
100 void blockdev_auto_del(BlockDriverState *bs)
101 {
102     DriveInfo *dinfo = drive_get_by_blockdev(bs);
103
104     if (dinfo && dinfo->auto_del) {
105         drive_put_ref(dinfo);
106     }
107 }
108
109 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
110 {
111     int max_devs = if_max_devs[type];
112     return max_devs ? index / max_devs : 0;
113 }
114
115 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
116 {
117     int max_devs = if_max_devs[type];
118     return max_devs ? index % max_devs : index;
119 }
120
121 QemuOpts *drive_def(const char *optstr)
122 {
123     return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
124 }
125
126 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
127                     const char *optstr)
128 {
129     QemuOpts *opts;
130     char buf[32];
131
132     opts = drive_def(optstr);
133     if (!opts) {
134         return NULL;
135     }
136     if (type != IF_DEFAULT) {
137         qemu_opt_set(opts, "if", if_name[type]);
138     }
139     if (index >= 0) {
140         snprintf(buf, sizeof(buf), "%d", index);
141         qemu_opt_set(opts, "index", buf);
142     }
143     if (file)
144         qemu_opt_set(opts, "file", file);
145     return opts;
146 }
147
148 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
149 {
150     DriveInfo *dinfo;
151
152     /* seek interface, bus and unit */
153
154     QTAILQ_FOREACH(dinfo, &drives, next) {
155         if (dinfo->type == type &&
156             dinfo->bus == bus &&
157             dinfo->unit == unit)
158             return dinfo;
159     }
160
161     return NULL;
162 }
163
164 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
165 {
166     return drive_get(type,
167                      drive_index_to_bus_id(type, index),
168                      drive_index_to_unit_id(type, index));
169 }
170
171 int drive_get_max_bus(BlockInterfaceType type)
172 {
173     int max_bus;
174     DriveInfo *dinfo;
175
176     max_bus = -1;
177     QTAILQ_FOREACH(dinfo, &drives, next) {
178         if(dinfo->type == type &&
179            dinfo->bus > max_bus)
180             max_bus = dinfo->bus;
181     }
182     return max_bus;
183 }
184
185 /* Get a block device.  This should only be used for single-drive devices
186    (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
187    appropriate bus.  */
188 DriveInfo *drive_get_next(BlockInterfaceType type)
189 {
190     static int next_block_unit[IF_COUNT];
191
192     return drive_get(type, 0, next_block_unit[type]++);
193 }
194
195 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
196 {
197     DriveInfo *dinfo;
198
199     QTAILQ_FOREACH(dinfo, &drives, next) {
200         if (dinfo->bdrv == bs) {
201             return dinfo;
202         }
203     }
204     return NULL;
205 }
206
207 static void bdrv_format_print(void *opaque, const char *name)
208 {
209     error_printf(" %s", name);
210 }
211
212 static void drive_uninit(DriveInfo *dinfo)
213 {
214     qemu_opts_del(dinfo->opts);
215     bdrv_delete(dinfo->bdrv);
216     g_free(dinfo->id);
217     QTAILQ_REMOVE(&drives, dinfo, next);
218     g_free(dinfo->serial);
219     g_free(dinfo);
220 }
221
222 void drive_put_ref(DriveInfo *dinfo)
223 {
224     assert(dinfo->refcount);
225     if (--dinfo->refcount == 0) {
226         drive_uninit(dinfo);
227     }
228 }
229
230 void drive_get_ref(DriveInfo *dinfo)
231 {
232     dinfo->refcount++;
233 }
234
235 typedef struct {
236     QEMUBH *bh;
237     DriveInfo *dinfo;
238 } DrivePutRefBH;
239
240 static void drive_put_ref_bh(void *opaque)
241 {
242     DrivePutRefBH *s = opaque;
243
244     drive_put_ref(s->dinfo);
245     qemu_bh_delete(s->bh);
246     g_free(s);
247 }
248
249 /*
250  * Release a drive reference in a BH
251  *
252  * It is not possible to use drive_put_ref() from a callback function when the
253  * callers still need the drive.  In such cases we schedule a BH to release the
254  * reference.
255  */
256 static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
257 {
258     DrivePutRefBH *s;
259
260     s = g_new(DrivePutRefBH, 1);
261     s->bh = qemu_bh_new(drive_put_ref_bh, s);
262     s->dinfo = dinfo;
263     qemu_bh_schedule(s->bh);
264 }
265
266 static int parse_block_error_action(const char *buf, bool is_read)
267 {
268     if (!strcmp(buf, "ignore")) {
269         return BLOCKDEV_ON_ERROR_IGNORE;
270     } else if (!is_read && !strcmp(buf, "enospc")) {
271         return BLOCKDEV_ON_ERROR_ENOSPC;
272     } else if (!strcmp(buf, "stop")) {
273         return BLOCKDEV_ON_ERROR_STOP;
274     } else if (!strcmp(buf, "report")) {
275         return BLOCKDEV_ON_ERROR_REPORT;
276     } else {
277         error_report("'%s' invalid %s error action",
278                      buf, is_read ? "read" : "write");
279         return -1;
280     }
281 }
282
283 static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
284 {
285     bool bps_flag;
286     bool iops_flag;
287
288     assert(io_limits);
289
290     bps_flag  = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
291                  && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
292                  || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
293     iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
294                  && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
295                  || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
296     if (bps_flag || iops_flag) {
297         error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
298                          "cannot be used at the same time");
299         return false;
300     }
301
302     if (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] < 0 ||
303         io_limits->bps[BLOCK_IO_LIMIT_WRITE] < 0 ||
304         io_limits->bps[BLOCK_IO_LIMIT_READ] < 0 ||
305         io_limits->iops[BLOCK_IO_LIMIT_TOTAL] < 0 ||
306         io_limits->iops[BLOCK_IO_LIMIT_WRITE] < 0 ||
307         io_limits->iops[BLOCK_IO_LIMIT_READ] < 0) {
308         error_setg(errp, "bps and iops values must be 0 or greater");
309         return false;
310     }
311
312     return true;
313 }
314
315 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
316 {
317     const char *buf;
318     const char *file = NULL;
319     const char *serial;
320     const char *mediastr = "";
321     BlockInterfaceType type;
322     enum { MEDIA_DISK, MEDIA_CDROM } media;
323     int bus_id, unit_id;
324     int cyls, heads, secs, translation;
325     BlockDriver *drv = NULL;
326     int max_devs;
327     int index;
328     int ro = 0;
329     int bdrv_flags = 0;
330     int on_read_error, on_write_error;
331     const char *devaddr;
332     DriveInfo *dinfo;
333     BlockIOLimit io_limits;
334     int snapshot = 0;
335     bool copy_on_read;
336     int ret;
337     Error *error = NULL;
338     QemuOpts *opts;
339     QDict *bs_opts;
340     const char *id;
341
342     translation = BIOS_ATA_TRANSLATION_AUTO;
343     media = MEDIA_DISK;
344
345     /* Check common options by copying from all_opts to opts, all other options
346      * are stored in bs_opts. */
347     id = qemu_opts_id(all_opts);
348     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
349     if (error_is_set(&error)) {
350         qerror_report_err(error);
351         error_free(error);
352         return NULL;
353     }
354
355     bs_opts = qdict_new();
356     qemu_opts_to_qdict(all_opts, bs_opts);
357     qemu_opts_absorb_qdict(opts, bs_opts, &error);
358     if (error_is_set(&error)) {
359         qerror_report_err(error);
360         error_free(error);
361         return NULL;
362     }
363
364     if (id) {
365         qdict_del(bs_opts, "id");
366     }
367
368     /* extract parameters */
369     bus_id  = qemu_opt_get_number(opts, "bus", 0);
370     unit_id = qemu_opt_get_number(opts, "unit", -1);
371     index   = qemu_opt_get_number(opts, "index", -1);
372
373     cyls  = qemu_opt_get_number(opts, "cyls", 0);
374     heads = qemu_opt_get_number(opts, "heads", 0);
375     secs  = qemu_opt_get_number(opts, "secs", 0);
376
377     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
378     ro = qemu_opt_get_bool(opts, "readonly", 0);
379     copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
380
381     file = qemu_opt_get(opts, "file");
382     serial = qemu_opt_get(opts, "serial");
383
384     if ((buf = qemu_opt_get(opts, "if")) != NULL) {
385         for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
386             ;
387         if (type == IF_COUNT) {
388             error_report("unsupported bus type '%s'", buf);
389             return NULL;
390         }
391     } else {
392         type = block_default_type;
393     }
394
395     max_devs = if_max_devs[type];
396
397     if (cyls || heads || secs) {
398         if (cyls < 1) {
399             error_report("invalid physical cyls number");
400             return NULL;
401         }
402         if (heads < 1) {
403             error_report("invalid physical heads number");
404             return NULL;
405         }
406         if (secs < 1) {
407             error_report("invalid physical secs number");
408             return NULL;
409         }
410     }
411
412     if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
413         if (!cyls) {
414             error_report("'%s' trans must be used with cyls, heads and secs",
415                          buf);
416             return NULL;
417         }
418         if (!strcmp(buf, "none"))
419             translation = BIOS_ATA_TRANSLATION_NONE;
420         else if (!strcmp(buf, "lba"))
421             translation = BIOS_ATA_TRANSLATION_LBA;
422         else if (!strcmp(buf, "auto"))
423             translation = BIOS_ATA_TRANSLATION_AUTO;
424         else {
425             error_report("'%s' invalid translation type", buf);
426             return NULL;
427         }
428     }
429
430     if ((buf = qemu_opt_get(opts, "media")) != NULL) {
431         if (!strcmp(buf, "disk")) {
432             media = MEDIA_DISK;
433         } else if (!strcmp(buf, "cdrom")) {
434             if (cyls || secs || heads) {
435                 error_report("CHS can't be set with media=%s", buf);
436                 return NULL;
437             }
438             media = MEDIA_CDROM;
439         } else {
440             error_report("'%s' invalid media", buf);
441             return NULL;
442         }
443     }
444
445     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
446         if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
447             error_report("invalid discard option");
448             return NULL;
449         }
450     }
451
452     bdrv_flags |= BDRV_O_CACHE_WB;
453     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
454         if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
455             error_report("invalid cache option");
456             return NULL;
457         }
458     }
459
460 #ifdef CONFIG_LINUX_AIO
461     if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
462         if (!strcmp(buf, "native")) {
463             bdrv_flags |= BDRV_O_NATIVE_AIO;
464         } else if (!strcmp(buf, "threads")) {
465             /* this is the default */
466         } else {
467            error_report("invalid aio option");
468            return NULL;
469         }
470     }
471 #endif
472
473     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
474         if (is_help_option(buf)) {
475             error_printf("Supported formats:");
476             bdrv_iterate_format(bdrv_format_print, NULL);
477             error_printf("\n");
478             return NULL;
479         }
480         drv = bdrv_find_whitelisted_format(buf);
481         if (!drv) {
482             error_report("'%s' invalid format", buf);
483             return NULL;
484         }
485     }
486
487     /* disk I/O throttling */
488     io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =
489                            qemu_opt_get_number(opts, "bps", 0);
490     io_limits.bps[BLOCK_IO_LIMIT_READ]   =
491                            qemu_opt_get_number(opts, "bps_rd", 0);
492     io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =
493                            qemu_opt_get_number(opts, "bps_wr", 0);
494     io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
495                            qemu_opt_get_number(opts, "iops", 0);
496     io_limits.iops[BLOCK_IO_LIMIT_READ]  =
497                            qemu_opt_get_number(opts, "iops_rd", 0);
498     io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
499                            qemu_opt_get_number(opts, "iops_wr", 0);
500
501     if (!do_check_io_limits(&io_limits, &error)) {
502         error_report("%s", error_get_pretty(error));
503         error_free(error);
504         return NULL;
505     }
506
507     if (qemu_opt_get(opts, "boot") != NULL) {
508         fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
509                 "ignored. Future versions will reject this parameter. Please "
510                 "update your scripts.\n");
511     }
512
513     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
514     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
515         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
516             error_report("werror is not supported by this bus type");
517             return NULL;
518         }
519
520         on_write_error = parse_block_error_action(buf, 0);
521         if (on_write_error < 0) {
522             return NULL;
523         }
524     }
525
526     on_read_error = BLOCKDEV_ON_ERROR_REPORT;
527     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
528         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
529             error_report("rerror is not supported by this bus type");
530             return NULL;
531         }
532
533         on_read_error = parse_block_error_action(buf, 1);
534         if (on_read_error < 0) {
535             return NULL;
536         }
537     }
538
539     if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
540         if (type != IF_VIRTIO) {
541             error_report("addr is not supported by this bus type");
542             return NULL;
543         }
544     }
545
546     /* compute bus and unit according index */
547
548     if (index != -1) {
549         if (bus_id != 0 || unit_id != -1) {
550             error_report("index cannot be used with bus and unit");
551             return NULL;
552         }
553         bus_id = drive_index_to_bus_id(type, index);
554         unit_id = drive_index_to_unit_id(type, index);
555     }
556
557     /* if user doesn't specify a unit_id,
558      * try to find the first free
559      */
560
561     if (unit_id == -1) {
562        unit_id = 0;
563        while (drive_get(type, bus_id, unit_id) != NULL) {
564            unit_id++;
565            if (max_devs && unit_id >= max_devs) {
566                unit_id -= max_devs;
567                bus_id++;
568            }
569        }
570     }
571
572     /* check unit id */
573
574     if (max_devs && unit_id >= max_devs) {
575         error_report("unit %d too big (max is %d)",
576                      unit_id, max_devs - 1);
577         return NULL;
578     }
579
580     /*
581      * catch multiple definitions
582      */
583
584     if (drive_get(type, bus_id, unit_id) != NULL) {
585         error_report("drive with bus=%d, unit=%d (index=%d) exists",
586                      bus_id, unit_id, index);
587         return NULL;
588     }
589
590     /* init */
591
592     dinfo = g_malloc0(sizeof(*dinfo));
593     if ((buf = qemu_opts_id(opts)) != NULL) {
594         dinfo->id = g_strdup(buf);
595     } else {
596         /* no id supplied -> create one */
597         dinfo->id = g_malloc0(32);
598         if (type == IF_IDE || type == IF_SCSI)
599             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
600         if (max_devs)
601             snprintf(dinfo->id, 32, "%s%i%s%i",
602                      if_name[type], bus_id, mediastr, unit_id);
603         else
604             snprintf(dinfo->id, 32, "%s%s%i",
605                      if_name[type], mediastr, unit_id);
606     }
607     dinfo->bdrv = bdrv_new(dinfo->id);
608     dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
609     dinfo->bdrv->read_only = ro;
610     dinfo->devaddr = devaddr;
611     dinfo->type = type;
612     dinfo->bus = bus_id;
613     dinfo->unit = unit_id;
614     dinfo->cyls = cyls;
615     dinfo->heads = heads;
616     dinfo->secs = secs;
617     dinfo->trans = translation;
618     dinfo->opts = all_opts;
619     dinfo->refcount = 1;
620     if (serial != NULL) {
621         dinfo->serial = g_strdup(serial);
622     }
623     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
624
625     bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
626
627     /* disk I/O throttling */
628     bdrv_set_io_limits(dinfo->bdrv, &io_limits);
629
630     switch(type) {
631     case IF_IDE:
632     case IF_SCSI:
633     case IF_XEN:
634     case IF_NONE:
635         dinfo->media_cd = media == MEDIA_CDROM;
636         break;
637     case IF_SD:
638     case IF_FLOPPY:
639     case IF_PFLASH:
640     case IF_MTD:
641         break;
642     case IF_VIRTIO:
643     {
644         /* add virtio block device */
645         QemuOpts *devopts;
646         devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
647         if (arch_type == QEMU_ARCH_S390X) {
648             qemu_opt_set(devopts, "driver", "virtio-blk-s390");
649         } else {
650             qemu_opt_set(devopts, "driver", "virtio-blk-pci");
651         }
652         qemu_opt_set(devopts, "drive", dinfo->id);
653         if (devaddr)
654             qemu_opt_set(devopts, "addr", devaddr);
655         break;
656     }
657     default:
658         abort();
659     }
660     if (!file || !*file) {
661         if (qdict_size(bs_opts)) {
662             file = NULL;
663         } else {
664             return dinfo;
665         }
666     }
667     if (snapshot) {
668         /* always use cache=unsafe with snapshot */
669         bdrv_flags &= ~BDRV_O_CACHE_MASK;
670         bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
671     }
672
673     if (copy_on_read) {
674         bdrv_flags |= BDRV_O_COPY_ON_READ;
675     }
676
677     if (runstate_check(RUN_STATE_INMIGRATE)) {
678         bdrv_flags |= BDRV_O_INCOMING;
679     }
680
681     if (media == MEDIA_CDROM) {
682         /* CDROM is fine for any interface, don't check.  */
683         ro = 1;
684     } else if (ro == 1) {
685         if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
686             type != IF_NONE && type != IF_PFLASH) {
687             error_report("readonly not supported by this bus type");
688             goto err;
689         }
690     }
691
692     bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
693
694     if (ro && copy_on_read) {
695         error_report("warning: disabling copy_on_read on readonly drive");
696     }
697
698     ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
699     bs_opts = NULL;
700
701     if (ret < 0) {
702         if (ret == -EMEDIUMTYPE) {
703             error_report("could not open disk image %s: not in %s format",
704                          file ?: dinfo->id, drv->format_name);
705         } else {
706             error_report("could not open disk image %s: %s",
707                          file ?: dinfo->id, strerror(-ret));
708         }
709         goto err;
710     }
711
712     if (bdrv_key_required(dinfo->bdrv))
713         autostart = 0;
714
715     qemu_opts_del(opts);
716
717     return dinfo;
718
719 err:
720     qemu_opts_del(opts);
721     QDECREF(bs_opts);
722     bdrv_delete(dinfo->bdrv);
723     g_free(dinfo->id);
724     QTAILQ_REMOVE(&drives, dinfo, next);
725     g_free(dinfo);
726     return NULL;
727 }
728
729 void do_commit(Monitor *mon, const QDict *qdict)
730 {
731     const char *device = qdict_get_str(qdict, "device");
732     BlockDriverState *bs;
733     int ret;
734
735     if (!strcmp(device, "all")) {
736         ret = bdrv_commit_all();
737     } else {
738         bs = bdrv_find(device);
739         if (!bs) {
740             monitor_printf(mon, "Device '%s' not found\n", device);
741             return;
742         }
743         ret = bdrv_commit(bs);
744     }
745     if (ret < 0) {
746         monitor_printf(mon, "'commit' error for '%s': %s\n", device,
747                        strerror(-ret));
748     }
749 }
750
751 static void blockdev_do_action(int kind, void *data, Error **errp)
752 {
753     BlockdevAction action;
754     BlockdevActionList list;
755
756     action.kind = kind;
757     action.data = data;
758     list.value = &action;
759     list.next = NULL;
760     qmp_transaction(&list, errp);
761 }
762
763 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
764                                 bool has_format, const char *format,
765                                 bool has_mode, enum NewImageMode mode,
766                                 Error **errp)
767 {
768     BlockdevSnapshot snapshot = {
769         .device = (char *) device,
770         .snapshot_file = (char *) snapshot_file,
771         .has_format = has_format,
772         .format = (char *) format,
773         .has_mode = has_mode,
774         .mode = mode,
775     };
776     blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
777                        errp);
778 }
779
780
781 /* New and old BlockDriverState structs for group snapshots */
782 typedef struct BlkTransactionStates {
783     BlockDriverState *old_bs;
784     BlockDriverState *new_bs;
785     QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
786 } BlkTransactionStates;
787
788 static void external_snapshot_prepare(BlockdevAction *action,
789                                       BlkTransactionStates *states,
790                                       Error **errp)
791 {
792     BlockDriver *proto_drv;
793     BlockDriver *drv;
794     int flags, ret;
795     Error *local_err = NULL;
796     const char *device;
797     const char *new_image_file;
798     const char *format = "qcow2";
799     enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
800
801     /* get parameters */
802     g_assert(action->kind == BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
803
804     device = action->blockdev_snapshot_sync->device;
805     new_image_file = action->blockdev_snapshot_sync->snapshot_file;
806     if (action->blockdev_snapshot_sync->has_format) {
807         format = action->blockdev_snapshot_sync->format;
808     }
809     if (action->blockdev_snapshot_sync->has_mode) {
810         mode = action->blockdev_snapshot_sync->mode;
811     }
812
813     /* start processing */
814     drv = bdrv_find_format(format);
815     if (!drv) {
816         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
817         return;
818     }
819
820     states->old_bs = bdrv_find(device);
821     if (!states->old_bs) {
822         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
823         return;
824     }
825
826     if (!bdrv_is_inserted(states->old_bs)) {
827         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
828         return;
829     }
830
831     if (bdrv_in_use(states->old_bs)) {
832         error_set(errp, QERR_DEVICE_IN_USE, device);
833         return;
834     }
835
836     if (!bdrv_is_read_only(states->old_bs)) {
837         if (bdrv_flush(states->old_bs)) {
838             error_set(errp, QERR_IO_ERROR);
839             return;
840         }
841     }
842
843     flags = states->old_bs->open_flags;
844
845     proto_drv = bdrv_find_protocol(new_image_file);
846     if (!proto_drv) {
847         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
848         return;
849     }
850
851     /* create new image w/backing file */
852     if (mode != NEW_IMAGE_MODE_EXISTING) {
853         bdrv_img_create(new_image_file, format,
854                         states->old_bs->filename,
855                         states->old_bs->drv->format_name,
856                         NULL, -1, flags, &local_err, false);
857         if (error_is_set(&local_err)) {
858             error_propagate(errp, local_err);
859             return;
860         }
861     }
862
863     /* We will manually add the backing_hd field to the bs later */
864     states->new_bs = bdrv_new("");
865     /* TODO Inherit bs->options or only take explicit options with an
866      * extended QMP command? */
867     ret = bdrv_open(states->new_bs, new_image_file, NULL,
868                     flags | BDRV_O_NO_BACKING, drv);
869     if (ret != 0) {
870         error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
871     }
872 }
873
874 /*
875  * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
876  *  then we do not pivot any of the devices in the group, and abandon the
877  *  snapshots
878  */
879 void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
880 {
881     BlockdevActionList *dev_entry = dev_list;
882     BlkTransactionStates *states, *next;
883     Error *local_err = NULL;
884
885     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
886     QSIMPLEQ_INIT(&snap_bdrv_states);
887
888     /* drain all i/o before any snapshots */
889     bdrv_drain_all();
890
891     /* We don't do anything in this loop that commits us to the snapshot */
892     while (NULL != dev_entry) {
893         BlockdevAction *dev_info = NULL;
894
895         dev_info = dev_entry->value;
896         dev_entry = dev_entry->next;
897
898         states = g_malloc0(sizeof(BlkTransactionStates));
899         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
900
901         switch (dev_info->kind) {
902         case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
903             external_snapshot_prepare(dev_info, states, errp);
904             if (error_is_set(&local_err)) {
905                 error_propagate(errp, local_err);
906                 goto delete_and_fail;
907             }
908             break;
909         default:
910             abort();
911         }
912
913     }
914
915
916     /* Now we are going to do the actual pivot.  Everything up to this point
917      * is reversible, but we are committed at this point */
918     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
919         /* This removes our old bs from the bdrv_states, and adds the new bs */
920         bdrv_append(states->new_bs, states->old_bs);
921         /* We don't need (or want) to use the transactional
922          * bdrv_reopen_multiple() across all the entries at once, because we
923          * don't want to abort all of them if one of them fails the reopen */
924         bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
925                     NULL);
926     }
927
928     /* success */
929     goto exit;
930
931 delete_and_fail:
932     /*
933     * failure, and it is all-or-none; abandon each new bs, and keep using
934     * the original bs for all images
935     */
936     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
937         if (states->new_bs) {
938              bdrv_delete(states->new_bs);
939         }
940     }
941 exit:
942     QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
943         g_free(states);
944     }
945 }
946
947
948 static void eject_device(BlockDriverState *bs, int force, Error **errp)
949 {
950     if (bdrv_in_use(bs)) {
951         error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
952         return;
953     }
954     if (!bdrv_dev_has_removable_media(bs)) {
955         error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
956         return;
957     }
958
959     if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
960         bdrv_dev_eject_request(bs, force);
961         if (!force) {
962             error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
963             return;
964         }
965     }
966
967     bdrv_close(bs);
968 }
969
970 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
971 {
972     BlockDriverState *bs;
973
974     bs = bdrv_find(device);
975     if (!bs) {
976         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
977         return;
978     }
979
980     eject_device(bs, force, errp);
981 }
982
983 void qmp_block_passwd(const char *device, const char *password, Error **errp)
984 {
985     BlockDriverState *bs;
986     int err;
987
988     bs = bdrv_find(device);
989     if (!bs) {
990         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
991         return;
992     }
993
994     err = bdrv_set_key(bs, password);
995     if (err == -EINVAL) {
996         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
997         return;
998     } else if (err < 0) {
999         error_set(errp, QERR_INVALID_PASSWORD);
1000         return;
1001     }
1002 }
1003
1004 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1005                                     int bdrv_flags, BlockDriver *drv,
1006                                     const char *password, Error **errp)
1007 {
1008     if (bdrv_open(bs, filename, NULL, bdrv_flags, drv) < 0) {
1009         error_set(errp, QERR_OPEN_FILE_FAILED, filename);
1010         return;
1011     }
1012
1013     if (bdrv_key_required(bs)) {
1014         if (password) {
1015             if (bdrv_set_key(bs, password) < 0) {
1016                 error_set(errp, QERR_INVALID_PASSWORD);
1017             }
1018         } else {
1019             error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1020                       bdrv_get_encrypted_filename(bs));
1021         }
1022     } else if (password) {
1023         error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1024     }
1025 }
1026
1027 void qmp_change_blockdev(const char *device, const char *filename,
1028                          bool has_format, const char *format, Error **errp)
1029 {
1030     BlockDriverState *bs;
1031     BlockDriver *drv = NULL;
1032     int bdrv_flags;
1033     Error *err = NULL;
1034
1035     bs = bdrv_find(device);
1036     if (!bs) {
1037         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1038         return;
1039     }
1040
1041     if (format) {
1042         drv = bdrv_find_whitelisted_format(format);
1043         if (!drv) {
1044             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1045             return;
1046         }
1047     }
1048
1049     eject_device(bs, 0, &err);
1050     if (error_is_set(&err)) {
1051         error_propagate(errp, err);
1052         return;
1053     }
1054
1055     bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1056     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1057
1058     qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1059 }
1060
1061 /* throttling disk I/O limits */
1062 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1063                                int64_t bps_wr, int64_t iops, int64_t iops_rd,
1064                                int64_t iops_wr, Error **errp)
1065 {
1066     BlockIOLimit io_limits;
1067     BlockDriverState *bs;
1068
1069     bs = bdrv_find(device);
1070     if (!bs) {
1071         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1072         return;
1073     }
1074
1075     io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
1076     io_limits.bps[BLOCK_IO_LIMIT_READ]  = bps_rd;
1077     io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
1078     io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1079     io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1080     io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
1081
1082     if (!do_check_io_limits(&io_limits, errp)) {
1083         return;
1084     }
1085
1086     bs->io_limits = io_limits;
1087
1088     if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1089         bdrv_io_limits_enable(bs);
1090     } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1091         bdrv_io_limits_disable(bs);
1092     } else {
1093         if (bs->block_timer) {
1094             qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1095         }
1096     }
1097 }
1098
1099 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1100 {
1101     const char *id = qdict_get_str(qdict, "id");
1102     BlockDriverState *bs;
1103
1104     bs = bdrv_find(id);
1105     if (!bs) {
1106         qerror_report(QERR_DEVICE_NOT_FOUND, id);
1107         return -1;
1108     }
1109     if (bdrv_in_use(bs)) {
1110         qerror_report(QERR_DEVICE_IN_USE, id);
1111         return -1;
1112     }
1113
1114     /* quiesce block driver; prevent further io */
1115     bdrv_drain_all();
1116     bdrv_flush(bs);
1117     bdrv_close(bs);
1118
1119     /* if we have a device attached to this BlockDriverState
1120      * then we need to make the drive anonymous until the device
1121      * can be removed.  If this is a drive with no device backing
1122      * then we can just get rid of the block driver state right here.
1123      */
1124     if (bdrv_get_attached_dev(bs)) {
1125         bdrv_make_anon(bs);
1126     } else {
1127         drive_uninit(drive_get_by_blockdev(bs));
1128     }
1129
1130     return 0;
1131 }
1132
1133 void qmp_block_resize(const char *device, int64_t size, Error **errp)
1134 {
1135     BlockDriverState *bs;
1136     int ret;
1137
1138     bs = bdrv_find(device);
1139     if (!bs) {
1140         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1141         return;
1142     }
1143
1144     if (size < 0) {
1145         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1146         return;
1147     }
1148
1149     /* complete all in-flight operations before resizing the device */
1150     bdrv_drain_all();
1151
1152     ret = bdrv_truncate(bs, size);
1153     switch (ret) {
1154     case 0:
1155         break;
1156     case -ENOMEDIUM:
1157         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1158         break;
1159     case -ENOTSUP:
1160         error_set(errp, QERR_UNSUPPORTED);
1161         break;
1162     case -EACCES:
1163         error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1164         break;
1165     case -EBUSY:
1166         error_set(errp, QERR_DEVICE_IN_USE, device);
1167         break;
1168     default:
1169         error_setg_errno(errp, -ret, "Could not resize");
1170         break;
1171     }
1172 }
1173
1174 static void block_job_cb(void *opaque, int ret)
1175 {
1176     BlockDriverState *bs = opaque;
1177     QObject *obj;
1178
1179     trace_block_job_cb(bs, bs->job, ret);
1180
1181     assert(bs->job);
1182     obj = qobject_from_block_job(bs->job);
1183     if (ret < 0) {
1184         QDict *dict = qobject_to_qdict(obj);
1185         qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1186     }
1187
1188     if (block_job_is_cancelled(bs->job)) {
1189         monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1190     } else {
1191         monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1192     }
1193     qobject_decref(obj);
1194
1195     drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
1196 }
1197
1198 void qmp_block_stream(const char *device, bool has_base,
1199                       const char *base, bool has_speed, int64_t speed,
1200                       bool has_on_error, BlockdevOnError on_error,
1201                       Error **errp)
1202 {
1203     BlockDriverState *bs;
1204     BlockDriverState *base_bs = NULL;
1205     Error *local_err = NULL;
1206
1207     if (!has_on_error) {
1208         on_error = BLOCKDEV_ON_ERROR_REPORT;
1209     }
1210
1211     bs = bdrv_find(device);
1212     if (!bs) {
1213         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1214         return;
1215     }
1216
1217     if (base) {
1218         base_bs = bdrv_find_backing_image(bs, base);
1219         if (base_bs == NULL) {
1220             error_set(errp, QERR_BASE_NOT_FOUND, base);
1221             return;
1222         }
1223     }
1224
1225     stream_start(bs, base_bs, base, has_speed ? speed : 0,
1226                  on_error, block_job_cb, bs, &local_err);
1227     if (error_is_set(&local_err)) {
1228         error_propagate(errp, local_err);
1229         return;
1230     }
1231
1232     /* Grab a reference so hotplug does not delete the BlockDriverState from
1233      * underneath us.
1234      */
1235     drive_get_ref(drive_get_by_blockdev(bs));
1236
1237     trace_qmp_block_stream(bs, bs->job);
1238 }
1239
1240 void qmp_block_commit(const char *device,
1241                       bool has_base, const char *base, const char *top,
1242                       bool has_speed, int64_t speed,
1243                       Error **errp)
1244 {
1245     BlockDriverState *bs;
1246     BlockDriverState *base_bs, *top_bs;
1247     Error *local_err = NULL;
1248     /* This will be part of the QMP command, if/when the
1249      * BlockdevOnError change for blkmirror makes it in
1250      */
1251     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1252
1253     /* drain all i/o before commits */
1254     bdrv_drain_all();
1255
1256     bs = bdrv_find(device);
1257     if (!bs) {
1258         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1259         return;
1260     }
1261
1262     /* default top_bs is the active layer */
1263     top_bs = bs;
1264
1265     if (top) {
1266         if (strcmp(bs->filename, top) != 0) {
1267             top_bs = bdrv_find_backing_image(bs, top);
1268         }
1269     }
1270
1271     if (top_bs == NULL) {
1272         error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1273         return;
1274     }
1275
1276     if (has_base && base) {
1277         base_bs = bdrv_find_backing_image(top_bs, base);
1278     } else {
1279         base_bs = bdrv_find_base(top_bs);
1280     }
1281
1282     if (base_bs == NULL) {
1283         error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1284         return;
1285     }
1286
1287     commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1288                 &local_err);
1289     if (local_err != NULL) {
1290         error_propagate(errp, local_err);
1291         return;
1292     }
1293     /* Grab a reference so hotplug does not delete the BlockDriverState from
1294      * underneath us.
1295      */
1296     drive_get_ref(drive_get_by_blockdev(bs));
1297 }
1298
1299 #define DEFAULT_MIRROR_BUF_SIZE   (10 << 20)
1300
1301 void qmp_drive_mirror(const char *device, const char *target,
1302                       bool has_format, const char *format,
1303                       enum MirrorSyncMode sync,
1304                       bool has_mode, enum NewImageMode mode,
1305                       bool has_speed, int64_t speed,
1306                       bool has_granularity, uint32_t granularity,
1307                       bool has_buf_size, int64_t buf_size,
1308                       bool has_on_source_error, BlockdevOnError on_source_error,
1309                       bool has_on_target_error, BlockdevOnError on_target_error,
1310                       Error **errp)
1311 {
1312     BlockDriverState *bs;
1313     BlockDriverState *source, *target_bs;
1314     BlockDriver *proto_drv;
1315     BlockDriver *drv = NULL;
1316     Error *local_err = NULL;
1317     int flags;
1318     uint64_t size;
1319     int ret;
1320
1321     if (!has_speed) {
1322         speed = 0;
1323     }
1324     if (!has_on_source_error) {
1325         on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1326     }
1327     if (!has_on_target_error) {
1328         on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1329     }
1330     if (!has_mode) {
1331         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1332     }
1333     if (!has_granularity) {
1334         granularity = 0;
1335     }
1336     if (!has_buf_size) {
1337         buf_size = DEFAULT_MIRROR_BUF_SIZE;
1338     }
1339
1340     if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1341         error_set(errp, QERR_INVALID_PARAMETER, device);
1342         return;
1343     }
1344     if (granularity & (granularity - 1)) {
1345         error_set(errp, QERR_INVALID_PARAMETER, device);
1346         return;
1347     }
1348
1349     bs = bdrv_find(device);
1350     if (!bs) {
1351         error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1352         return;
1353     }
1354
1355     if (!bdrv_is_inserted(bs)) {
1356         error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1357         return;
1358     }
1359
1360     if (!has_format) {
1361         format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1362     }
1363     if (format) {
1364         drv = bdrv_find_format(format);
1365         if (!drv) {
1366             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1367             return;
1368         }
1369     }
1370
1371     if (bdrv_in_use(bs)) {
1372         error_set(errp, QERR_DEVICE_IN_USE, device);
1373         return;
1374     }
1375
1376     flags = bs->open_flags | BDRV_O_RDWR;
1377     source = bs->backing_hd;
1378     if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1379         sync = MIRROR_SYNC_MODE_FULL;
1380     }
1381
1382     proto_drv = bdrv_find_protocol(target);
1383     if (!proto_drv) {
1384         error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1385         return;
1386     }
1387
1388     bdrv_get_geometry(bs, &size);
1389     size *= 512;
1390     if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1391         /* create new image w/o backing file */
1392         assert(format && drv);
1393         bdrv_img_create(target, format,
1394                         NULL, NULL, NULL, size, flags, &local_err, false);
1395     } else {
1396         switch (mode) {
1397         case NEW_IMAGE_MODE_EXISTING:
1398             ret = 0;
1399             break;
1400         case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1401             /* create new image with backing file */
1402             bdrv_img_create(target, format,
1403                             source->filename,
1404                             source->drv->format_name,
1405                             NULL, size, flags, &local_err, false);
1406             break;
1407         default:
1408             abort();
1409         }
1410     }
1411
1412     if (error_is_set(&local_err)) {
1413         error_propagate(errp, local_err);
1414         return;
1415     }
1416
1417     /* Mirroring takes care of copy-on-write using the source's backing
1418      * file.
1419      */
1420     target_bs = bdrv_new("");
1421     ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
1422
1423     if (ret < 0) {
1424         bdrv_delete(target_bs);
1425         error_set(errp, QERR_OPEN_FILE_FAILED, target);
1426         return;
1427     }
1428
1429     mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1430                  on_source_error, on_target_error,
1431                  block_job_cb, bs, &local_err);
1432     if (local_err != NULL) {
1433         bdrv_delete(target_bs);
1434         error_propagate(errp, local_err);
1435         return;
1436     }
1437
1438     /* Grab a reference so hotplug does not delete the BlockDriverState from
1439      * underneath us.
1440      */
1441     drive_get_ref(drive_get_by_blockdev(bs));
1442 }
1443
1444 static BlockJob *find_block_job(const char *device)
1445 {
1446     BlockDriverState *bs;
1447
1448     bs = bdrv_find(device);
1449     if (!bs || !bs->job) {
1450         return NULL;
1451     }
1452     return bs->job;
1453 }
1454
1455 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
1456 {
1457     BlockJob *job = find_block_job(device);
1458
1459     if (!job) {
1460         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1461         return;
1462     }
1463
1464     block_job_set_speed(job, speed, errp);
1465 }
1466
1467 void qmp_block_job_cancel(const char *device,
1468                           bool has_force, bool force, Error **errp)
1469 {
1470     BlockJob *job = find_block_job(device);
1471
1472     if (!has_force) {
1473         force = false;
1474     }
1475
1476     if (!job) {
1477         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1478         return;
1479     }
1480     if (job->paused && !force) {
1481         error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1482         return;
1483     }
1484
1485     trace_qmp_block_job_cancel(job);
1486     block_job_cancel(job);
1487 }
1488
1489 void qmp_block_job_pause(const char *device, Error **errp)
1490 {
1491     BlockJob *job = find_block_job(device);
1492
1493     if (!job) {
1494         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1495         return;
1496     }
1497
1498     trace_qmp_block_job_pause(job);
1499     block_job_pause(job);
1500 }
1501
1502 void qmp_block_job_resume(const char *device, Error **errp)
1503 {
1504     BlockJob *job = find_block_job(device);
1505
1506     if (!job) {
1507         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1508         return;
1509     }
1510
1511     trace_qmp_block_job_resume(job);
1512     block_job_resume(job);
1513 }
1514
1515 void qmp_block_job_complete(const char *device, Error **errp)
1516 {
1517     BlockJob *job = find_block_job(device);
1518
1519     if (!job) {
1520         error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1521         return;
1522     }
1523
1524     trace_qmp_block_job_complete(job);
1525     block_job_complete(job, errp);
1526 }
1527
1528 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1529 {
1530     BlockJobInfoList **prev = opaque;
1531     BlockJob *job = bs->job;
1532
1533     if (job) {
1534         BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1535         elem->value = block_job_query(bs->job);
1536         (*prev)->next = elem;
1537         *prev = elem;
1538     }
1539 }
1540
1541 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1542 {
1543     /* Dummy is a fake list element for holding the head pointer */
1544     BlockJobInfoList dummy = {};
1545     BlockJobInfoList *prev = &dummy;
1546     bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1547     return dummy.next;
1548 }
1549
1550 QemuOptsList qemu_common_drive_opts = {
1551     .name = "drive",
1552     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
1553     .desc = {
1554         {
1555             .name = "bus",
1556             .type = QEMU_OPT_NUMBER,
1557             .help = "bus number",
1558         },{
1559             .name = "unit",
1560             .type = QEMU_OPT_NUMBER,
1561             .help = "unit number (i.e. lun for scsi)",
1562         },{
1563             .name = "if",
1564             .type = QEMU_OPT_STRING,
1565             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1566         },{
1567             .name = "index",
1568             .type = QEMU_OPT_NUMBER,
1569             .help = "index number",
1570         },{
1571             .name = "cyls",
1572             .type = QEMU_OPT_NUMBER,
1573             .help = "number of cylinders (ide disk geometry)",
1574         },{
1575             .name = "heads",
1576             .type = QEMU_OPT_NUMBER,
1577             .help = "number of heads (ide disk geometry)",
1578         },{
1579             .name = "secs",
1580             .type = QEMU_OPT_NUMBER,
1581             .help = "number of sectors (ide disk geometry)",
1582         },{
1583             .name = "trans",
1584             .type = QEMU_OPT_STRING,
1585             .help = "chs translation (auto, lba. none)",
1586         },{
1587             .name = "media",
1588             .type = QEMU_OPT_STRING,
1589             .help = "media type (disk, cdrom)",
1590         },{
1591             .name = "snapshot",
1592             .type = QEMU_OPT_BOOL,
1593             .help = "enable/disable snapshot mode",
1594         },{
1595             .name = "file",
1596             .type = QEMU_OPT_STRING,
1597             .help = "disk image",
1598         },{
1599             .name = "discard",
1600             .type = QEMU_OPT_STRING,
1601             .help = "discard operation (ignore/off, unmap/on)",
1602         },{
1603             .name = "cache",
1604             .type = QEMU_OPT_STRING,
1605             .help = "host cache usage (none, writeback, writethrough, "
1606                     "directsync, unsafe)",
1607         },{
1608             .name = "aio",
1609             .type = QEMU_OPT_STRING,
1610             .help = "host AIO implementation (threads, native)",
1611         },{
1612             .name = "format",
1613             .type = QEMU_OPT_STRING,
1614             .help = "disk format (raw, qcow2, ...)",
1615         },{
1616             .name = "serial",
1617             .type = QEMU_OPT_STRING,
1618             .help = "disk serial number",
1619         },{
1620             .name = "rerror",
1621             .type = QEMU_OPT_STRING,
1622             .help = "read error action",
1623         },{
1624             .name = "werror",
1625             .type = QEMU_OPT_STRING,
1626             .help = "write error action",
1627         },{
1628             .name = "addr",
1629             .type = QEMU_OPT_STRING,
1630             .help = "pci address (virtio only)",
1631         },{
1632             .name = "readonly",
1633             .type = QEMU_OPT_BOOL,
1634             .help = "open drive file as read-only",
1635         },{
1636             .name = "iops",
1637             .type = QEMU_OPT_NUMBER,
1638             .help = "limit total I/O operations per second",
1639         },{
1640             .name = "iops_rd",
1641             .type = QEMU_OPT_NUMBER,
1642             .help = "limit read operations per second",
1643         },{
1644             .name = "iops_wr",
1645             .type = QEMU_OPT_NUMBER,
1646             .help = "limit write operations per second",
1647         },{
1648             .name = "bps",
1649             .type = QEMU_OPT_NUMBER,
1650             .help = "limit total bytes per second",
1651         },{
1652             .name = "bps_rd",
1653             .type = QEMU_OPT_NUMBER,
1654             .help = "limit read bytes per second",
1655         },{
1656             .name = "bps_wr",
1657             .type = QEMU_OPT_NUMBER,
1658             .help = "limit write bytes per second",
1659         },{
1660             .name = "copy-on-read",
1661             .type = QEMU_OPT_BOOL,
1662             .help = "copy read data from backing file into image file",
1663         },{
1664             .name = "boot",
1665             .type = QEMU_OPT_BOOL,
1666             .help = "(deprecated, ignored)",
1667         },
1668         { /* end of list */ }
1669     },
1670 };
1671
1672 QemuOptsList qemu_drive_opts = {
1673     .name = "drive",
1674     .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
1675     .desc = {
1676         {
1677             .name = "bus",
1678             .type = QEMU_OPT_NUMBER,
1679             .help = "bus number",
1680         },{
1681             .name = "unit",
1682             .type = QEMU_OPT_NUMBER,
1683             .help = "unit number (i.e. lun for scsi)",
1684         },{
1685             .name = "if",
1686             .type = QEMU_OPT_STRING,
1687             .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1688         },{
1689             .name = "index",
1690             .type = QEMU_OPT_NUMBER,
1691             .help = "index number",
1692         },{
1693             .name = "cyls",
1694             .type = QEMU_OPT_NUMBER,
1695             .help = "number of cylinders (ide disk geometry)",
1696         },{
1697             .name = "heads",
1698             .type = QEMU_OPT_NUMBER,
1699             .help = "number of heads (ide disk geometry)",
1700         },{
1701             .name = "secs",
1702             .type = QEMU_OPT_NUMBER,
1703             .help = "number of sectors (ide disk geometry)",
1704         },{
1705             .name = "trans",
1706             .type = QEMU_OPT_STRING,
1707             .help = "chs translation (auto, lba. none)",
1708         },{
1709             .name = "media",
1710             .type = QEMU_OPT_STRING,
1711             .help = "media type (disk, cdrom)",
1712         },{
1713             .name = "snapshot",
1714             .type = QEMU_OPT_BOOL,
1715             .help = "enable/disable snapshot mode",
1716         },{
1717             .name = "file",
1718             .type = QEMU_OPT_STRING,
1719             .help = "disk image",
1720         },{
1721             .name = "discard",
1722             .type = QEMU_OPT_STRING,
1723             .help = "discard operation (ignore/off, unmap/on)",
1724         },{
1725             .name = "cache",
1726             .type = QEMU_OPT_STRING,
1727             .help = "host cache usage (none, writeback, writethrough, "
1728                     "directsync, unsafe)",
1729         },{
1730             .name = "aio",
1731             .type = QEMU_OPT_STRING,
1732             .help = "host AIO implementation (threads, native)",
1733         },{
1734             .name = "format",
1735             .type = QEMU_OPT_STRING,
1736             .help = "disk format (raw, qcow2, ...)",
1737         },{
1738             .name = "serial",
1739             .type = QEMU_OPT_STRING,
1740             .help = "disk serial number",
1741         },{
1742             .name = "rerror",
1743             .type = QEMU_OPT_STRING,
1744             .help = "read error action",
1745         },{
1746             .name = "werror",
1747             .type = QEMU_OPT_STRING,
1748             .help = "write error action",
1749         },{
1750             .name = "addr",
1751             .type = QEMU_OPT_STRING,
1752             .help = "pci address (virtio only)",
1753         },{
1754             .name = "readonly",
1755             .type = QEMU_OPT_BOOL,
1756             .help = "open drive file as read-only",
1757         },{
1758             .name = "iops",
1759             .type = QEMU_OPT_NUMBER,
1760             .help = "limit total I/O operations per second",
1761         },{
1762             .name = "iops_rd",
1763             .type = QEMU_OPT_NUMBER,
1764             .help = "limit read operations per second",
1765         },{
1766             .name = "iops_wr",
1767             .type = QEMU_OPT_NUMBER,
1768             .help = "limit write operations per second",
1769         },{
1770             .name = "bps",
1771             .type = QEMU_OPT_NUMBER,
1772             .help = "limit total bytes per second",
1773         },{
1774             .name = "bps_rd",
1775             .type = QEMU_OPT_NUMBER,
1776             .help = "limit read bytes per second",
1777         },{
1778             .name = "bps_wr",
1779             .type = QEMU_OPT_NUMBER,
1780             .help = "limit write bytes per second",
1781         },{
1782             .name = "copy-on-read",
1783             .type = QEMU_OPT_BOOL,
1784             .help = "copy read data from backing file into image file",
1785         },{
1786             .name = "boot",
1787             .type = QEMU_OPT_BOOL,
1788             .help = "(deprecated, ignored)",
1789         },
1790         { /* end of list */ }
1791     },
1792 };
This page took 0.1166 seconds and 4 git commands to generate.