Statistics
| Branch: | Revision:

root / blockdev.c @ 73f5e313

History | View | Annotate | Download (24 kB)

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"
17
#include "block_int.h"
18

    
19
static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
20

    
21
static const char *const if_name[IF_COUNT] = {
22
    [IF_NONE] = "none",
23
    [IF_IDE] = "ide",
24
    [IF_SCSI] = "scsi",
25
    [IF_FLOPPY] = "floppy",
26
    [IF_PFLASH] = "pflash",
27
    [IF_MTD] = "mtd",
28
    [IF_SD] = "sd",
29
    [IF_VIRTIO] = "virtio",
30
    [IF_XEN] = "xen",
31
};
32

    
33
static const int if_max_devs[IF_COUNT] = {
34
    /*
35
     * Do not change these numbers!  They govern how drive option
36
     * index maps to unit and bus.  That mapping is ABI.
37
     *
38
     * All controllers used to imlement if=T drives need to support
39
     * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
40
     * Otherwise, some index values map to "impossible" bus, unit
41
     * values.
42
     *
43
     * For instance, if you change [IF_SCSI] to 255, -drive
44
     * if=scsi,index=12 no longer means bus=1,unit=5, but
45
     * bus=0,unit=12.  With an lsi53c895a controller (7 units max),
46
     * the drive can't be set up.  Regression.
47
     */
48
    [IF_IDE] = 2,
49
    [IF_SCSI] = 7,
50
};
51

    
52
/*
53
 * We automatically delete the drive when a device using it gets
54
 * unplugged.  Questionable feature, but we can't just drop it.
55
 * Device models call blockdev_mark_auto_del() to schedule the
56
 * automatic deletion, and generic qdev code calls blockdev_auto_del()
57
 * when deletion is actually safe.
58
 */
59
void blockdev_mark_auto_del(BlockDriverState *bs)
60
{
61
    DriveInfo *dinfo = drive_get_by_blockdev(bs);
62

    
63
    if (dinfo) {
64
        dinfo->auto_del = 1;
65
    }
66
}
67

    
68
void blockdev_auto_del(BlockDriverState *bs)
69
{
70
    DriveInfo *dinfo = drive_get_by_blockdev(bs);
71

    
72
    if (dinfo && dinfo->auto_del) {
73
        drive_put_ref(dinfo);
74
    }
75
}
76

    
77
static int drive_index_to_bus_id(BlockInterfaceType type, int index)
78
{
79
    int max_devs = if_max_devs[type];
80
    return max_devs ? index / max_devs : 0;
81
}
82

    
83
static int drive_index_to_unit_id(BlockInterfaceType type, int index)
84
{
85
    int max_devs = if_max_devs[type];
86
    return max_devs ? index % max_devs : index;
87
}
88

    
89
QemuOpts *drive_def(const char *optstr)
90
{
91
    return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
92
}
93

    
94
QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
95
                    const char *optstr)
96
{
97
    QemuOpts *opts;
98
    char buf[32];
99

    
100
    opts = drive_def(optstr);
101
    if (!opts) {
102
        return NULL;
103
    }
104
    if (type != IF_DEFAULT) {
105
        qemu_opt_set(opts, "if", if_name[type]);
106
    }
107
    if (index >= 0) {
108
        snprintf(buf, sizeof(buf), "%d", index);
109
        qemu_opt_set(opts, "index", buf);
110
    }
111
    if (file)
112
        qemu_opt_set(opts, "file", file);
113
    return opts;
114
}
115

    
116
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
117
{
118
    DriveInfo *dinfo;
119

    
120
    /* seek interface, bus and unit */
121

    
122
    QTAILQ_FOREACH(dinfo, &drives, next) {
123
        if (dinfo->type == type &&
124
            dinfo->bus == bus &&
125
            dinfo->unit == unit)
126
            return dinfo;
127
    }
128

    
129
    return NULL;
130
}
131

    
132
DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
133
{
134
    return drive_get(type,
135
                     drive_index_to_bus_id(type, index),
136
                     drive_index_to_unit_id(type, index));
137
}
138

    
139
int drive_get_max_bus(BlockInterfaceType type)
140
{
141
    int max_bus;
142
    DriveInfo *dinfo;
143

    
144
    max_bus = -1;
145
    QTAILQ_FOREACH(dinfo, &drives, next) {
146
        if(dinfo->type == type &&
147
           dinfo->bus > max_bus)
148
            max_bus = dinfo->bus;
149
    }
150
    return max_bus;
151
}
152

    
153
/* Get a block device.  This should only be used for single-drive devices
154
   (e.g. SD/Floppy/MTD).  Multi-disk devices (scsi/ide) should use the
155
   appropriate bus.  */
156
DriveInfo *drive_get_next(BlockInterfaceType type)
157
{
158
    static int next_block_unit[IF_COUNT];
159

    
160
    return drive_get(type, 0, next_block_unit[type]++);
161
}
162

    
163
DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
164
{
165
    DriveInfo *dinfo;
166

    
167
    QTAILQ_FOREACH(dinfo, &drives, next) {
168
        if (dinfo->bdrv == bs) {
169
            return dinfo;
170
        }
171
    }
172
    return NULL;
173
}
174

    
175
static void bdrv_format_print(void *opaque, const char *name)
176
{
177
    error_printf(" %s", name);
178
}
179

    
180
static void drive_uninit(DriveInfo *dinfo)
181
{
182
    qemu_opts_del(dinfo->opts);
183
    bdrv_delete(dinfo->bdrv);
184
    g_free(dinfo->id);
185
    QTAILQ_REMOVE(&drives, dinfo, next);
186
    g_free(dinfo);
187
}
188

    
189
void drive_put_ref(DriveInfo *dinfo)
190
{
191
    assert(dinfo->refcount);
192
    if (--dinfo->refcount == 0) {
193
        drive_uninit(dinfo);
194
    }
195
}
196

    
197
void drive_get_ref(DriveInfo *dinfo)
198
{
199
    dinfo->refcount++;
200
}
201

    
202
static int parse_block_error_action(const char *buf, int is_read)
203
{
204
    if (!strcmp(buf, "ignore")) {
205
        return BLOCK_ERR_IGNORE;
206
    } else if (!is_read && !strcmp(buf, "enospc")) {
207
        return BLOCK_ERR_STOP_ENOSPC;
208
    } else if (!strcmp(buf, "stop")) {
209
        return BLOCK_ERR_STOP_ANY;
210
    } else if (!strcmp(buf, "report")) {
211
        return BLOCK_ERR_REPORT;
212
    } else {
213
        error_report("'%s' invalid %s error action",
214
                     buf, is_read ? "read" : "write");
215
        return -1;
216
    }
217
}
218

    
219
static bool do_check_io_limits(BlockIOLimit *io_limits)
220
{
221
    bool bps_flag;
222
    bool iops_flag;
223

    
224
    assert(io_limits);
225

    
226
    bps_flag  = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
227
                 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
228
                 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
229
    iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
230
                 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
231
                 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
232
    if (bps_flag || iops_flag) {
233
        return false;
234
    }
235

    
236
    return true;
237
}
238

    
239
DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
240
{
241
    const char *buf;
242
    const char *file = NULL;
243
    char devname[128];
244
    const char *serial;
245
    const char *mediastr = "";
246
    BlockInterfaceType type;
247
    enum { MEDIA_DISK, MEDIA_CDROM } media;
248
    int bus_id, unit_id;
249
    int cyls, heads, secs, translation;
250
    BlockDriver *drv = NULL;
251
    int max_devs;
252
    int index;
253
    int ro = 0;
254
    int bdrv_flags = 0;
255
    int on_read_error, on_write_error;
256
    const char *devaddr;
257
    DriveInfo *dinfo;
258
    BlockIOLimit io_limits;
259
    int snapshot = 0;
260
    bool copy_on_read;
261
    int ret;
262

    
263
    translation = BIOS_ATA_TRANSLATION_AUTO;
264
    media = MEDIA_DISK;
265

    
266
    /* extract parameters */
267
    bus_id  = qemu_opt_get_number(opts, "bus", 0);
268
    unit_id = qemu_opt_get_number(opts, "unit", -1);
269
    index   = qemu_opt_get_number(opts, "index", -1);
270

    
271
    cyls  = qemu_opt_get_number(opts, "cyls", 0);
272
    heads = qemu_opt_get_number(opts, "heads", 0);
273
    secs  = qemu_opt_get_number(opts, "secs", 0);
274

    
275
    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
276
    ro = qemu_opt_get_bool(opts, "readonly", 0);
277
    copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
278

    
279
    file = qemu_opt_get(opts, "file");
280
    serial = qemu_opt_get(opts, "serial");
281

    
282
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
283
        pstrcpy(devname, sizeof(devname), buf);
284
        for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
285
            ;
286
        if (type == IF_COUNT) {
287
            error_report("unsupported bus type '%s'", buf);
288
            return NULL;
289
        }
290
    } else {
291
        type = default_to_scsi ? IF_SCSI : IF_IDE;
292
        pstrcpy(devname, sizeof(devname), if_name[type]);
293
    }
294

    
295
    max_devs = if_max_devs[type];
296

    
297
    if (cyls || heads || secs) {
298
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
299
            error_report("invalid physical cyls number");
300
            return NULL;
301
        }
302
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
303
            error_report("invalid physical heads number");
304
            return NULL;
305
        }
306
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
307
            error_report("invalid physical secs number");
308
            return NULL;
309
        }
310
    }
311

    
312
    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
313
        if (!cyls) {
314
            error_report("'%s' trans must be used with cyls, heads and secs",
315
                         buf);
316
            return NULL;
317
        }
318
        if (!strcmp(buf, "none"))
319
            translation = BIOS_ATA_TRANSLATION_NONE;
320
        else if (!strcmp(buf, "lba"))
321
            translation = BIOS_ATA_TRANSLATION_LBA;
322
        else if (!strcmp(buf, "auto"))
323
            translation = BIOS_ATA_TRANSLATION_AUTO;
324
        else {
325
            error_report("'%s' invalid translation type", buf);
326
            return NULL;
327
        }
328
    }
329

    
330
    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
331
        if (!strcmp(buf, "disk")) {
332
            media = MEDIA_DISK;
333
        } else if (!strcmp(buf, "cdrom")) {
334
            if (cyls || secs || heads) {
335
                error_report("CHS can't be set with media=%s", buf);
336
                return NULL;
337
            }
338
            media = MEDIA_CDROM;
339
        } else {
340
            error_report("'%s' invalid media", buf);
341
            return NULL;
342
        }
343
    }
344

    
345
    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
346
        if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
347
            error_report("invalid cache option");
348
            return NULL;
349
        }
350
    }
351

    
352
#ifdef CONFIG_LINUX_AIO
353
    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
354
        if (!strcmp(buf, "native")) {
355
            bdrv_flags |= BDRV_O_NATIVE_AIO;
356
        } else if (!strcmp(buf, "threads")) {
357
            /* this is the default */
358
        } else {
359
           error_report("invalid aio option");
360
           return NULL;
361
        }
362
    }
363
#endif
364

    
365
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
366
       if (strcmp(buf, "?") == 0) {
367
           error_printf("Supported formats:");
368
           bdrv_iterate_format(bdrv_format_print, NULL);
369
           error_printf("\n");
370
           return NULL;
371
        }
372
        drv = bdrv_find_whitelisted_format(buf);
373
        if (!drv) {
374
            error_report("'%s' invalid format", buf);
375
            return NULL;
376
        }
377
    }
378

    
379
    /* disk I/O throttling */
380
    io_limits.bps[BLOCK_IO_LIMIT_TOTAL]  =
381
                           qemu_opt_get_number(opts, "bps", 0);
382
    io_limits.bps[BLOCK_IO_LIMIT_READ]   =
383
                           qemu_opt_get_number(opts, "bps_rd", 0);
384
    io_limits.bps[BLOCK_IO_LIMIT_WRITE]  =
385
                           qemu_opt_get_number(opts, "bps_wr", 0);
386
    io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
387
                           qemu_opt_get_number(opts, "iops", 0);
388
    io_limits.iops[BLOCK_IO_LIMIT_READ]  =
389
                           qemu_opt_get_number(opts, "iops_rd", 0);
390
    io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
391
                           qemu_opt_get_number(opts, "iops_wr", 0);
392

    
393
    if (!do_check_io_limits(&io_limits)) {
394
        error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
395
                     "cannot be used at the same time");
396
        return NULL;
397
    }
398

    
399
    on_write_error = BLOCK_ERR_STOP_ENOSPC;
400
    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
401
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
402
            error_report("werror is not supported by this bus type");
403
            return NULL;
404
        }
405

    
406
        on_write_error = parse_block_error_action(buf, 0);
407
        if (on_write_error < 0) {
408
            return NULL;
409
        }
410
    }
411

    
412
    on_read_error = BLOCK_ERR_REPORT;
413
    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
414
        if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
415
            error_report("rerror is not supported by this bus type");
416
            return NULL;
417
        }
418

    
419
        on_read_error = parse_block_error_action(buf, 1);
420
        if (on_read_error < 0) {
421
            return NULL;
422
        }
423
    }
424

    
425
    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
426
        if (type != IF_VIRTIO) {
427
            error_report("addr is not supported by this bus type");
428
            return NULL;
429
        }
430
    }
431

    
432
    /* compute bus and unit according index */
433

    
434
    if (index != -1) {
435
        if (bus_id != 0 || unit_id != -1) {
436
            error_report("index cannot be used with bus and unit");
437
            return NULL;
438
        }
439
        bus_id = drive_index_to_bus_id(type, index);
440
        unit_id = drive_index_to_unit_id(type, index);
441
    }
442

    
443
    /* if user doesn't specify a unit_id,
444
     * try to find the first free
445
     */
446

    
447
    if (unit_id == -1) {
448
       unit_id = 0;
449
       while (drive_get(type, bus_id, unit_id) != NULL) {
450
           unit_id++;
451
           if (max_devs && unit_id >= max_devs) {
452
               unit_id -= max_devs;
453
               bus_id++;
454
           }
455
       }
456
    }
457

    
458
    /* check unit id */
459

    
460
    if (max_devs && unit_id >= max_devs) {
461
        error_report("unit %d too big (max is %d)",
462
                     unit_id, max_devs - 1);
463
        return NULL;
464
    }
465

    
466
    /*
467
     * catch multiple definitions
468
     */
469

    
470
    if (drive_get(type, bus_id, unit_id) != NULL) {
471
        error_report("drive with bus=%d, unit=%d (index=%d) exists",
472
                     bus_id, unit_id, index);
473
        return NULL;
474
    }
475

    
476
    /* init */
477

    
478
    dinfo = g_malloc0(sizeof(*dinfo));
479
    if ((buf = qemu_opts_id(opts)) != NULL) {
480
        dinfo->id = g_strdup(buf);
481
    } else {
482
        /* no id supplied -> create one */
483
        dinfo->id = g_malloc0(32);
484
        if (type == IF_IDE || type == IF_SCSI)
485
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
486
        if (max_devs)
487
            snprintf(dinfo->id, 32, "%s%i%s%i",
488
                     devname, bus_id, mediastr, unit_id);
489
        else
490
            snprintf(dinfo->id, 32, "%s%s%i",
491
                     devname, mediastr, unit_id);
492
    }
493
    dinfo->bdrv = bdrv_new(dinfo->id);
494
    dinfo->devaddr = devaddr;
495
    dinfo->type = type;
496
    dinfo->bus = bus_id;
497
    dinfo->unit = unit_id;
498
    dinfo->opts = opts;
499
    dinfo->refcount = 1;
500
    if (serial)
501
        strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
502
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
503

    
504
    bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
505

    
506
    /* disk I/O throttling */
507
    bdrv_set_io_limits(dinfo->bdrv, &io_limits);
508

    
509
    switch(type) {
510
    case IF_IDE:
511
    case IF_SCSI:
512
    case IF_XEN:
513
    case IF_NONE:
514
        switch(media) {
515
        case MEDIA_DISK:
516
            if (cyls != 0) {
517
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
518
                bdrv_set_translation_hint(dinfo->bdrv, translation);
519
            }
520
            break;
521
        case MEDIA_CDROM:
522
            dinfo->media_cd = 1;
523
            break;
524
        }
525
        break;
526
    case IF_SD:
527
    case IF_FLOPPY:
528
    case IF_PFLASH:
529
    case IF_MTD:
530
        break;
531
    case IF_VIRTIO:
532
        /* add virtio block device */
533
        opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
534
        qemu_opt_set(opts, "driver", "virtio-blk");
535
        qemu_opt_set(opts, "drive", dinfo->id);
536
        if (devaddr)
537
            qemu_opt_set(opts, "addr", devaddr);
538
        break;
539
    default:
540
        abort();
541
    }
542
    if (!file || !*file) {
543
        return dinfo;
544
    }
545
    if (snapshot) {
546
        /* always use cache=unsafe with snapshot */
547
        bdrv_flags &= ~BDRV_O_CACHE_MASK;
548
        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
549
    }
550

    
551
    if (copy_on_read) {
552
        bdrv_flags |= BDRV_O_COPY_ON_READ;
553
    }
554

    
555
    if (media == MEDIA_CDROM) {
556
        /* CDROM is fine for any interface, don't check.  */
557
        ro = 1;
558
    } else if (ro == 1) {
559
        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
560
            error_report("readonly not supported by this bus type");
561
            goto err;
562
        }
563
    }
564

    
565
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
566

    
567
    ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
568
    if (ret < 0) {
569
        error_report("could not open disk image %s: %s",
570
                     file, strerror(-ret));
571
        goto err;
572
    }
573

    
574
    if (bdrv_key_required(dinfo->bdrv))
575
        autostart = 0;
576
    return dinfo;
577

    
578
err:
579
    bdrv_delete(dinfo->bdrv);
580
    g_free(dinfo->id);
581
    QTAILQ_REMOVE(&drives, dinfo, next);
582
    g_free(dinfo);
583
    return NULL;
584
}
585

    
586
void do_commit(Monitor *mon, const QDict *qdict)
587
{
588
    const char *device = qdict_get_str(qdict, "device");
589
    BlockDriverState *bs;
590

    
591
    if (!strcmp(device, "all")) {
592
        bdrv_commit_all();
593
    } else {
594
        bs = bdrv_find(device);
595
        if (!bs) {
596
            qerror_report(QERR_DEVICE_NOT_FOUND, device);
597
            return;
598
        }
599
        bdrv_commit(bs);
600
    }
601
}
602

    
603
int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
604
{
605
    const char *device = qdict_get_str(qdict, "device");
606
    const char *filename = qdict_get_try_str(qdict, "snapshot-file");
607
    const char *format = qdict_get_try_str(qdict, "format");
608
    BlockDriverState *bs;
609
    BlockDriver *drv, *old_drv, *proto_drv;
610
    int ret = 0;
611
    int flags;
612
    char old_filename[1024];
613

    
614
    if (!filename) {
615
        qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
616
        ret = -1;
617
        goto out;
618
    }
619

    
620
    bs = bdrv_find(device);
621
    if (!bs) {
622
        qerror_report(QERR_DEVICE_NOT_FOUND, device);
623
        ret = -1;
624
        goto out;
625
    }
626

    
627
    pstrcpy(old_filename, sizeof(old_filename), bs->filename);
628

    
629
    old_drv = bs->drv;
630
    flags = bs->open_flags;
631

    
632
    if (!format) {
633
        format = "qcow2";
634
    }
635

    
636
    drv = bdrv_find_format(format);
637
    if (!drv) {
638
        qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
639
        ret = -1;
640
        goto out;
641
    }
642

    
643
    proto_drv = bdrv_find_protocol(filename);
644
    if (!proto_drv) {
645
        qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
646
        ret = -1;
647
        goto out;
648
    }
649

    
650
    ret = bdrv_img_create(filename, format, bs->filename,
651
                          bs->drv->format_name, NULL, -1, flags);
652
    if (ret) {
653
        goto out;
654
    }
655

    
656
    bdrv_drain_all();
657
    bdrv_flush(bs);
658

    
659
    bdrv_close(bs);
660
    ret = bdrv_open(bs, filename, flags, drv);
661
    /*
662
     * If reopening the image file we just created fails, fall back
663
     * and try to re-open the original image. If that fails too, we
664
     * are in serious trouble.
665
     */
666
    if (ret != 0) {
667
        ret = bdrv_open(bs, old_filename, flags, old_drv);
668
        if (ret != 0) {
669
            qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
670
        } else {
671
            qerror_report(QERR_OPEN_FILE_FAILED, filename);
672
        }
673
    }
674
out:
675
    if (ret) {
676
        ret = -1;
677
    }
678

    
679
    return ret;
680
}
681

    
682
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
683
{
684
    if (!bdrv_dev_has_removable_media(bs)) {
685
        qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
686
        return -1;
687
    }
688
    if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
689
        bdrv_dev_eject_request(bs, force);
690
        if (!force) {
691
            qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
692
            return -1;
693
        }
694
    }
695
    bdrv_close(bs);
696
    return 0;
697
}
698

    
699
int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
700
{
701
    BlockDriverState *bs;
702
    int force = qdict_get_try_bool(qdict, "force", 0);
703
    const char *filename = qdict_get_str(qdict, "device");
704

    
705
    bs = bdrv_find(filename);
706
    if (!bs) {
707
        qerror_report(QERR_DEVICE_NOT_FOUND, filename);
708
        return -1;
709
    }
710
    return eject_device(mon, bs, force);
711
}
712

    
713
int do_block_set_passwd(Monitor *mon, const QDict *qdict,
714
                        QObject **ret_data)
715
{
716
    BlockDriverState *bs;
717
    int err;
718

    
719
    bs = bdrv_find(qdict_get_str(qdict, "device"));
720
    if (!bs) {
721
        qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
722
        return -1;
723
    }
724

    
725
    err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
726
    if (err == -EINVAL) {
727
        qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
728
        return -1;
729
    } else if (err < 0) {
730
        qerror_report(QERR_INVALID_PASSWORD);
731
        return -1;
732
    }
733

    
734
    return 0;
735
}
736

    
737
int do_change_block(Monitor *mon, const char *device,
738
                    const char *filename, const char *fmt)
739
{
740
    BlockDriverState *bs;
741
    BlockDriver *drv = NULL;
742
    int bdrv_flags;
743

    
744
    bs = bdrv_find(device);
745
    if (!bs) {
746
        qerror_report(QERR_DEVICE_NOT_FOUND, device);
747
        return -1;
748
    }
749
    if (fmt) {
750
        drv = bdrv_find_whitelisted_format(fmt);
751
        if (!drv) {
752
            qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
753
            return -1;
754
        }
755
    }
756
    if (eject_device(mon, bs, 0) < 0) {
757
        return -1;
758
    }
759
    bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
760
    bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
761
    if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
762
        qerror_report(QERR_OPEN_FILE_FAILED, filename);
763
        return -1;
764
    }
765
    return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
766
}
767

    
768
/* throttling disk I/O limits */
769
int do_block_set_io_throttle(Monitor *mon,
770
                       const QDict *qdict, QObject **ret_data)
771
{
772
    BlockIOLimit io_limits;
773
    const char *devname = qdict_get_str(qdict, "device");
774
    BlockDriverState *bs;
775

    
776
    io_limits.bps[BLOCK_IO_LIMIT_TOTAL]
777
                        = qdict_get_try_int(qdict, "bps", -1);
778
    io_limits.bps[BLOCK_IO_LIMIT_READ]
779
                        = qdict_get_try_int(qdict, "bps_rd", -1);
780
    io_limits.bps[BLOCK_IO_LIMIT_WRITE]
781
                        = qdict_get_try_int(qdict, "bps_wr", -1);
782
    io_limits.iops[BLOCK_IO_LIMIT_TOTAL]
783
                        = qdict_get_try_int(qdict, "iops", -1);
784
    io_limits.iops[BLOCK_IO_LIMIT_READ]
785
                        = qdict_get_try_int(qdict, "iops_rd", -1);
786
    io_limits.iops[BLOCK_IO_LIMIT_WRITE]
787
                        = qdict_get_try_int(qdict, "iops_wr", -1);
788

    
789
    bs = bdrv_find(devname);
790
    if (!bs) {
791
        qerror_report(QERR_DEVICE_NOT_FOUND, devname);
792
        return -1;
793
    }
794

    
795
    if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1)
796
        || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1)
797
        || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1)
798
        || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1)
799
        || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1)
800
        || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) {
801
        qerror_report(QERR_MISSING_PARAMETER,
802
                      "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr");
803
        return -1;
804
    }
805

    
806
    if (!do_check_io_limits(&io_limits)) {
807
        qerror_report(QERR_INVALID_PARAMETER_COMBINATION);
808
        return -1;
809
    }
810

    
811
    bs->io_limits = io_limits;
812
    bs->slice_time = BLOCK_IO_SLICE_TIME;
813

    
814
    if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
815
        bdrv_io_limits_enable(bs);
816
    } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
817
        bdrv_io_limits_disable(bs);
818
    } else {
819
        if (bs->block_timer) {
820
            qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
821
        }
822
    }
823

    
824
    return 0;
825
}
826

    
827
int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
828
{
829
    const char *id = qdict_get_str(qdict, "id");
830
    BlockDriverState *bs;
831

    
832
    bs = bdrv_find(id);
833
    if (!bs) {
834
        qerror_report(QERR_DEVICE_NOT_FOUND, id);
835
        return -1;
836
    }
837
    if (bdrv_in_use(bs)) {
838
        qerror_report(QERR_DEVICE_IN_USE, id);
839
        return -1;
840
    }
841

    
842
    /* quiesce block driver; prevent further io */
843
    bdrv_drain_all();
844
    bdrv_flush(bs);
845
    bdrv_close(bs);
846

    
847
    /* if we have a device attached to this BlockDriverState
848
     * then we need to make the drive anonymous until the device
849
     * can be removed.  If this is a drive with no device backing
850
     * then we can just get rid of the block driver state right here.
851
     */
852
    if (bdrv_get_attached_dev(bs)) {
853
        bdrv_make_anon(bs);
854
    } else {
855
        drive_uninit(drive_get_by_blockdev(bs));
856
    }
857

    
858
    return 0;
859
}
860

    
861
/*
862
 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
863
 * existing QERR_ macro mess is cleaned up.  A good example for better
864
 * error reports can be found in the qemu-img resize code.
865
 */
866
int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
867
{
868
    const char *device = qdict_get_str(qdict, "device");
869
    int64_t size = qdict_get_int(qdict, "size");
870
    BlockDriverState *bs;
871

    
872
    bs = bdrv_find(device);
873
    if (!bs) {
874
        qerror_report(QERR_DEVICE_NOT_FOUND, device);
875
        return -1;
876
    }
877

    
878
    if (size < 0) {
879
        qerror_report(QERR_UNDEFINED_ERROR);
880
        return -1;
881
    }
882

    
883
    if (bdrv_truncate(bs, size)) {
884
        qerror_report(QERR_UNDEFINED_ERROR);
885
        return -1;
886
    }
887

    
888
    return 0;
889
}