Annotation of qemu/blockdev.c, revision 1.1.1.4

1.1       root        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"
1.1.1.2   root       17: #include "hw/qdev.h"
                     18: #include "block_int.h"
1.1       root       19: 
                     20: static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
                     21: 
1.1.1.2   root       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] = {
                     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,
                     51: };
                     52: 
1.1       root       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: 
1.1.1.2   root       64:     if (dinfo) {
                     65:         dinfo->auto_del = 1;
                     66:     }
1.1       root       67: }
                     68: 
                     69: void blockdev_auto_del(BlockDriverState *bs)
                     70: {
                     71:     DriveInfo *dinfo = drive_get_by_blockdev(bs);
                     72: 
1.1.1.2   root       73:     if (dinfo && dinfo->auto_del) {
                     74:         drive_put_ref(dinfo);
1.1       root       75:     }
                     76: }
                     77: 
1.1.1.2   root       78: static int drive_index_to_bus_id(BlockInterfaceType type, int index)
1.1       root       79: {
1.1.1.2   root       80:     int max_devs = if_max_devs[type];
                     81:     return max_devs ? index / max_devs : 0;
                     82: }
1.1       root       83: 
1.1.1.2   root       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: }
1.1       root       89: 
1.1.1.2   root       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,
                     96:                     const char *optstr)
                     97: {
                     98:     QemuOpts *opts;
                     99:     char buf[32];
                    100: 
                    101:     opts = drive_def(optstr);
1.1       root      102:     if (!opts) {
                    103:         return NULL;
                    104:     }
1.1.1.2   root      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:     }
1.1       root      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: 
1.1.1.2   root      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: 
1.1       root      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: 
1.1.1.2   root      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: 
1.1       root      164: DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
                    165: {
                    166:     DriveInfo *dinfo;
                    167: 
                    168:     QTAILQ_FOREACH(dinfo, &drives, next) {
                    169:         if (dinfo->bdrv == bs) {
                    170:             return dinfo;
                    171:         }
                    172:     }
                    173:     return NULL;
                    174: }
                    175: 
                    176: static void bdrv_format_print(void *opaque, const char *name)
                    177: {
1.1.1.2   root      178:     error_printf(" %s", name);
1.1       root      179: }
                    180: 
1.1.1.2   root      181: static void drive_uninit(DriveInfo *dinfo)
1.1       root      182: {
                    183:     qemu_opts_del(dinfo->opts);
                    184:     bdrv_delete(dinfo->bdrv);
1.1.1.2   root      185:     qemu_free(dinfo->id);
1.1       root      186:     QTAILQ_REMOVE(&drives, dinfo, next);
                    187:     qemu_free(dinfo);
                    188: }
                    189: 
1.1.1.2   root      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: 
1.1       root      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 {
1.1.1.2   root      214:         error_report("'%s' invalid %s error action",
                    215:                      buf, is_read ? "read" : "write");
1.1       root      216:         return -1;
                    217:     }
                    218: }
                    219: 
1.1.1.2   root      220: DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
1.1       root      221: {
                    222:     const char *buf;
                    223:     const char *file = NULL;
                    224:     char devname[128];
                    225:     const char *serial;
                    226:     const char *mediastr = "";
                    227:     BlockInterfaceType type;
                    228:     enum { MEDIA_DISK, MEDIA_CDROM } media;
                    229:     int bus_id, unit_id;
                    230:     int cyls, heads, secs, translation;
                    231:     BlockDriver *drv = NULL;
                    232:     int max_devs;
                    233:     int index;
                    234:     int ro = 0;
                    235:     int bdrv_flags = 0;
                    236:     int on_read_error, on_write_error;
                    237:     const char *devaddr;
                    238:     DriveInfo *dinfo;
                    239:     int snapshot = 0;
                    240:     int ret;
                    241: 
                    242:     translation = BIOS_ATA_TRANSLATION_AUTO;
                    243:     media = MEDIA_DISK;
                    244: 
                    245:     /* extract parameters */
                    246:     bus_id  = qemu_opt_get_number(opts, "bus", 0);
                    247:     unit_id = qemu_opt_get_number(opts, "unit", -1);
                    248:     index   = qemu_opt_get_number(opts, "index", -1);
                    249: 
                    250:     cyls  = qemu_opt_get_number(opts, "cyls", 0);
                    251:     heads = qemu_opt_get_number(opts, "heads", 0);
                    252:     secs  = qemu_opt_get_number(opts, "secs", 0);
                    253: 
                    254:     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
                    255:     ro = qemu_opt_get_bool(opts, "readonly", 0);
                    256: 
                    257:     file = qemu_opt_get(opts, "file");
                    258:     serial = qemu_opt_get(opts, "serial");
                    259: 
                    260:     if ((buf = qemu_opt_get(opts, "if")) != NULL) {
                    261:         pstrcpy(devname, sizeof(devname), buf);
1.1.1.2   root      262:         for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
                    263:             ;
                    264:         if (type == IF_COUNT) {
                    265:             error_report("unsupported bus type '%s'", buf);
1.1       root      266:             return NULL;
                    267:        }
1.1.1.4 ! root      268:     } else {
        !           269:         type = default_to_scsi ? IF_SCSI : IF_IDE;
        !           270:         pstrcpy(devname, sizeof(devname), if_name[type]);
1.1       root      271:     }
1.1.1.4 ! root      272: 
1.1.1.2   root      273:     max_devs = if_max_devs[type];
1.1       root      274: 
                    275:     if (cyls || heads || secs) {
                    276:         if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
1.1.1.2   root      277:             error_report("invalid physical cyls number");
1.1       root      278:            return NULL;
                    279:        }
                    280:         if (heads < 1 || (type == IF_IDE && heads > 16)) {
1.1.1.2   root      281:             error_report("invalid physical heads number");
1.1       root      282:            return NULL;
                    283:        }
                    284:         if (secs < 1 || (type == IF_IDE && secs > 63)) {
1.1.1.2   root      285:             error_report("invalid physical secs number");
1.1       root      286:            return NULL;
                    287:        }
                    288:     }
                    289: 
                    290:     if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
                    291:         if (!cyls) {
1.1.1.4 ! root      292:             error_report("'%s' trans must be used with cyls, heads and secs",
1.1.1.2   root      293:                          buf);
1.1       root      294:             return NULL;
                    295:         }
                    296:         if (!strcmp(buf, "none"))
                    297:             translation = BIOS_ATA_TRANSLATION_NONE;
                    298:         else if (!strcmp(buf, "lba"))
                    299:             translation = BIOS_ATA_TRANSLATION_LBA;
                    300:         else if (!strcmp(buf, "auto"))
                    301:             translation = BIOS_ATA_TRANSLATION_AUTO;
                    302:        else {
1.1.1.2   root      303:             error_report("'%s' invalid translation type", buf);
1.1       root      304:            return NULL;
                    305:        }
                    306:     }
                    307: 
                    308:     if ((buf = qemu_opt_get(opts, "media")) != NULL) {
                    309:         if (!strcmp(buf, "disk")) {
                    310:            media = MEDIA_DISK;
                    311:        } else if (!strcmp(buf, "cdrom")) {
                    312:             if (cyls || secs || heads) {
1.1.1.4 ! root      313:                 error_report("CHS can't be set with media=%s", buf);
1.1       root      314:                return NULL;
                    315:             }
                    316:            media = MEDIA_CDROM;
                    317:        } else {
1.1.1.2   root      318:            error_report("'%s' invalid media", buf);
1.1       root      319:            return NULL;
                    320:        }
                    321:     }
                    322: 
                    323:     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
                    324:         if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
1.1.1.4 ! root      325:             bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1.1       root      326:         } else if (!strcmp(buf, "writeback")) {
                    327:             bdrv_flags |= BDRV_O_CACHE_WB;
                    328:         } else if (!strcmp(buf, "unsafe")) {
                    329:             bdrv_flags |= BDRV_O_CACHE_WB;
                    330:             bdrv_flags |= BDRV_O_NO_FLUSH;
                    331:         } else if (!strcmp(buf, "writethrough")) {
                    332:             /* this is the default */
                    333:         } else {
1.1.1.2   root      334:            error_report("invalid cache option");
1.1       root      335:            return NULL;
                    336:         }
                    337:     }
                    338: 
                    339: #ifdef CONFIG_LINUX_AIO
                    340:     if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
                    341:         if (!strcmp(buf, "native")) {
                    342:             bdrv_flags |= BDRV_O_NATIVE_AIO;
                    343:         } else if (!strcmp(buf, "threads")) {
                    344:             /* this is the default */
                    345:         } else {
1.1.1.2   root      346:            error_report("invalid aio option");
1.1       root      347:            return NULL;
                    348:         }
                    349:     }
                    350: #endif
                    351: 
                    352:     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
                    353:        if (strcmp(buf, "?") == 0) {
1.1.1.2   root      354:            error_printf("Supported formats:");
                    355:            bdrv_iterate_format(bdrv_format_print, NULL);
                    356:            error_printf("\n");
                    357:            return NULL;
1.1       root      358:         }
                    359:         drv = bdrv_find_whitelisted_format(buf);
                    360:         if (!drv) {
1.1.1.2   root      361:             error_report("'%s' invalid format", buf);
1.1       root      362:             return NULL;
                    363:         }
                    364:     }
                    365: 
                    366:     on_write_error = BLOCK_ERR_STOP_ENOSPC;
                    367:     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
                    368:         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
1.1.1.2   root      369:             error_report("werror is not supported by this bus type");
1.1       root      370:             return NULL;
                    371:         }
                    372: 
                    373:         on_write_error = parse_block_error_action(buf, 0);
                    374:         if (on_write_error < 0) {
                    375:             return NULL;
                    376:         }
                    377:     }
                    378: 
                    379:     on_read_error = BLOCK_ERR_REPORT;
                    380:     if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
1.1.1.2   root      381:         if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
                    382:             error_report("rerror is not supported by this bus type");
1.1       root      383:             return NULL;
                    384:         }
                    385: 
                    386:         on_read_error = parse_block_error_action(buf, 1);
                    387:         if (on_read_error < 0) {
                    388:             return NULL;
                    389:         }
                    390:     }
                    391: 
                    392:     if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
                    393:         if (type != IF_VIRTIO) {
1.1.1.2   root      394:             error_report("addr is not supported by this bus type");
1.1       root      395:             return NULL;
                    396:         }
                    397:     }
                    398: 
                    399:     /* compute bus and unit according index */
                    400: 
                    401:     if (index != -1) {
                    402:         if (bus_id != 0 || unit_id != -1) {
1.1.1.2   root      403:             error_report("index cannot be used with bus and unit");
1.1       root      404:             return NULL;
                    405:         }
1.1.1.2   root      406:         bus_id = drive_index_to_bus_id(type, index);
                    407:         unit_id = drive_index_to_unit_id(type, index);
1.1       root      408:     }
                    409: 
                    410:     /* if user doesn't specify a unit_id,
                    411:      * try to find the first free
                    412:      */
                    413: 
                    414:     if (unit_id == -1) {
                    415:        unit_id = 0;
                    416:        while (drive_get(type, bus_id, unit_id) != NULL) {
                    417:            unit_id++;
                    418:            if (max_devs && unit_id >= max_devs) {
                    419:                unit_id -= max_devs;
                    420:                bus_id++;
                    421:            }
                    422:        }
                    423:     }
                    424: 
                    425:     /* check unit id */
                    426: 
                    427:     if (max_devs && unit_id >= max_devs) {
1.1.1.2   root      428:         error_report("unit %d too big (max is %d)",
                    429:                      unit_id, max_devs - 1);
1.1       root      430:         return NULL;
                    431:     }
                    432: 
                    433:     /*
1.1.1.2   root      434:      * catch multiple definitions
1.1       root      435:      */
                    436: 
                    437:     if (drive_get(type, bus_id, unit_id) != NULL) {
1.1.1.2   root      438:         error_report("drive with bus=%d, unit=%d (index=%d) exists",
                    439:                      bus_id, unit_id, index);
1.1       root      440:         return NULL;
                    441:     }
                    442: 
                    443:     /* init */
                    444: 
                    445:     dinfo = qemu_mallocz(sizeof(*dinfo));
                    446:     if ((buf = qemu_opts_id(opts)) != NULL) {
                    447:         dinfo->id = qemu_strdup(buf);
                    448:     } else {
                    449:         /* no id supplied -> create one */
                    450:         dinfo->id = qemu_mallocz(32);
                    451:         if (type == IF_IDE || type == IF_SCSI)
                    452:             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
                    453:         if (max_devs)
                    454:             snprintf(dinfo->id, 32, "%s%i%s%i",
                    455:                      devname, bus_id, mediastr, unit_id);
                    456:         else
                    457:             snprintf(dinfo->id, 32, "%s%s%i",
                    458:                      devname, mediastr, unit_id);
                    459:     }
                    460:     dinfo->bdrv = bdrv_new(dinfo->id);
                    461:     dinfo->devaddr = devaddr;
                    462:     dinfo->type = type;
                    463:     dinfo->bus = bus_id;
                    464:     dinfo->unit = unit_id;
                    465:     dinfo->opts = opts;
1.1.1.2   root      466:     dinfo->refcount = 1;
1.1       root      467:     if (serial)
                    468:         strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
                    469:     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
                    470: 
                    471:     bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
                    472: 
                    473:     switch(type) {
                    474:     case IF_IDE:
                    475:     case IF_SCSI:
                    476:     case IF_XEN:
                    477:     case IF_NONE:
                    478:         switch(media) {
                    479:        case MEDIA_DISK:
                    480:             if (cyls != 0) {
                    481:                 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
                    482:                 bdrv_set_translation_hint(dinfo->bdrv, translation);
                    483:             }
                    484:            break;
                    485:        case MEDIA_CDROM:
1.1.1.4 ! root      486:             bdrv_set_removable(dinfo->bdrv, 1);
        !           487:             dinfo->media_cd = 1;
1.1       root      488:            break;
                    489:        }
                    490:         break;
                    491:     case IF_SD:
                    492:         /* FIXME: This isn't really a floppy, but it's a reasonable
                    493:            approximation.  */
                    494:     case IF_FLOPPY:
1.1.1.4 ! root      495:         bdrv_set_removable(dinfo->bdrv, 1);
1.1       root      496:         break;
                    497:     case IF_PFLASH:
                    498:     case IF_MTD:
                    499:         break;
                    500:     case IF_VIRTIO:
                    501:         /* add virtio block device */
1.1.1.2   root      502:         opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1.1.1.4 ! root      503:         qemu_opt_set(opts, "driver", "virtio-blk");
1.1       root      504:         qemu_opt_set(opts, "drive", dinfo->id);
                    505:         if (devaddr)
                    506:             qemu_opt_set(opts, "addr", devaddr);
                    507:         break;
1.1.1.2   root      508:     default:
1.1       root      509:         abort();
                    510:     }
                    511:     if (!file || !*file) {
1.1.1.2   root      512:         return dinfo;
1.1       root      513:     }
                    514:     if (snapshot) {
                    515:         /* always use cache=unsafe with snapshot */
                    516:         bdrv_flags &= ~BDRV_O_CACHE_MASK;
                    517:         bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
                    518:     }
                    519: 
                    520:     if (media == MEDIA_CDROM) {
                    521:         /* CDROM is fine for any interface, don't check.  */
                    522:         ro = 1;
                    523:     } else if (ro == 1) {
                    524:         if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1.1.1.2   root      525:             error_report("readonly not supported by this bus type");
                    526:             goto err;
1.1       root      527:         }
                    528:     }
                    529: 
                    530:     bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
                    531: 
                    532:     ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
                    533:     if (ret < 0) {
1.1.1.2   root      534:         error_report("could not open disk image %s: %s",
                    535:                      file, strerror(-ret));
                    536:         goto err;
1.1       root      537:     }
                    538: 
                    539:     if (bdrv_key_required(dinfo->bdrv))
                    540:         autostart = 0;
                    541:     return dinfo;
1.1.1.2   root      542: 
                    543: err:
                    544:     bdrv_delete(dinfo->bdrv);
                    545:     qemu_free(dinfo->id);
                    546:     QTAILQ_REMOVE(&drives, dinfo, next);
                    547:     qemu_free(dinfo);
                    548:     return NULL;
1.1       root      549: }
                    550: 
                    551: void do_commit(Monitor *mon, const QDict *qdict)
                    552: {
                    553:     const char *device = qdict_get_str(qdict, "device");
                    554:     BlockDriverState *bs;
                    555: 
                    556:     if (!strcmp(device, "all")) {
                    557:         bdrv_commit_all();
                    558:     } else {
                    559:         bs = bdrv_find(device);
                    560:         if (!bs) {
                    561:             qerror_report(QERR_DEVICE_NOT_FOUND, device);
                    562:             return;
                    563:         }
                    564:         bdrv_commit(bs);
                    565:     }
                    566: }
                    567: 
1.1.1.2   root      568: int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
                    569: {
                    570:     const char *device = qdict_get_str(qdict, "device");
1.1.1.4 ! root      571:     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1.1.1.2   root      572:     const char *format = qdict_get_try_str(qdict, "format");
                    573:     BlockDriverState *bs;
1.1.1.4 ! root      574:     BlockDriver *drv, *old_drv, *proto_drv;
1.1.1.2   root      575:     int ret = 0;
                    576:     int flags;
1.1.1.4 ! root      577:     char old_filename[1024];
1.1.1.2   root      578: 
                    579:     if (!filename) {
1.1.1.4 ! root      580:         qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
1.1.1.2   root      581:         ret = -1;
                    582:         goto out;
                    583:     }
                    584: 
                    585:     bs = bdrv_find(device);
                    586:     if (!bs) {
                    587:         qerror_report(QERR_DEVICE_NOT_FOUND, device);
                    588:         ret = -1;
                    589:         goto out;
                    590:     }
                    591: 
1.1.1.4 ! root      592:     pstrcpy(old_filename, sizeof(old_filename), bs->filename);
        !           593: 
        !           594:     old_drv = bs->drv;
        !           595:     flags = bs->open_flags;
        !           596: 
1.1.1.2   root      597:     if (!format) {
                    598:         format = "qcow2";
                    599:     }
                    600: 
                    601:     drv = bdrv_find_format(format);
                    602:     if (!drv) {
                    603:         qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
                    604:         ret = -1;
                    605:         goto out;
                    606:     }
                    607: 
                    608:     proto_drv = bdrv_find_protocol(filename);
                    609:     if (!proto_drv) {
                    610:         qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
                    611:         ret = -1;
                    612:         goto out;
                    613:     }
                    614: 
                    615:     ret = bdrv_img_create(filename, format, bs->filename,
1.1.1.4 ! root      616:                           bs->drv->format_name, NULL, -1, flags);
1.1.1.2   root      617:     if (ret) {
                    618:         goto out;
                    619:     }
                    620: 
                    621:     qemu_aio_flush();
                    622:     bdrv_flush(bs);
                    623: 
                    624:     bdrv_close(bs);
                    625:     ret = bdrv_open(bs, filename, flags, drv);
                    626:     /*
1.1.1.4 ! root      627:      * If reopening the image file we just created fails, fall back
        !           628:      * and try to re-open the original image. If that fails too, we
        !           629:      * are in serious trouble.
1.1.1.2   root      630:      */
                    631:     if (ret != 0) {
1.1.1.4 ! root      632:         ret = bdrv_open(bs, old_filename, flags, old_drv);
        !           633:         if (ret != 0) {
        !           634:             qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
        !           635:         } else {
        !           636:             qerror_report(QERR_OPEN_FILE_FAILED, filename);
        !           637:         }
1.1.1.2   root      638:     }
                    639: out:
                    640:     if (ret) {
                    641:         ret = -1;
                    642:     }
                    643: 
                    644:     return ret;
                    645: }
                    646: 
1.1       root      647: static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
                    648: {
                    649:     if (!force) {
                    650:         if (!bdrv_is_removable(bs)) {
                    651:             qerror_report(QERR_DEVICE_NOT_REMOVABLE,
                    652:                            bdrv_get_device_name(bs));
                    653:             return -1;
                    654:         }
                    655:         if (bdrv_is_locked(bs)) {
                    656:             qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
                    657:             return -1;
                    658:         }
                    659:     }
                    660:     bdrv_close(bs);
                    661:     return 0;
                    662: }
                    663: 
                    664: int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
                    665: {
                    666:     BlockDriverState *bs;
                    667:     int force = qdict_get_try_bool(qdict, "force", 0);
                    668:     const char *filename = qdict_get_str(qdict, "device");
                    669: 
                    670:     bs = bdrv_find(filename);
                    671:     if (!bs) {
                    672:         qerror_report(QERR_DEVICE_NOT_FOUND, filename);
                    673:         return -1;
                    674:     }
                    675:     return eject_device(mon, bs, force);
                    676: }
                    677: 
                    678: int do_block_set_passwd(Monitor *mon, const QDict *qdict,
                    679:                         QObject **ret_data)
                    680: {
                    681:     BlockDriverState *bs;
                    682:     int err;
                    683: 
                    684:     bs = bdrv_find(qdict_get_str(qdict, "device"));
                    685:     if (!bs) {
                    686:         qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
                    687:         return -1;
                    688:     }
                    689: 
                    690:     err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
                    691:     if (err == -EINVAL) {
                    692:         qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
                    693:         return -1;
                    694:     } else if (err < 0) {
                    695:         qerror_report(QERR_INVALID_PASSWORD);
                    696:         return -1;
                    697:     }
                    698: 
                    699:     return 0;
                    700: }
                    701: 
                    702: int do_change_block(Monitor *mon, const char *device,
                    703:                     const char *filename, const char *fmt)
                    704: {
                    705:     BlockDriverState *bs;
                    706:     BlockDriver *drv = NULL;
                    707:     int bdrv_flags;
                    708: 
                    709:     bs = bdrv_find(device);
                    710:     if (!bs) {
                    711:         qerror_report(QERR_DEVICE_NOT_FOUND, device);
                    712:         return -1;
                    713:     }
                    714:     if (fmt) {
                    715:         drv = bdrv_find_whitelisted_format(fmt);
                    716:         if (!drv) {
                    717:             qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
                    718:             return -1;
                    719:         }
                    720:     }
                    721:     if (eject_device(mon, bs, 0) < 0) {
                    722:         return -1;
                    723:     }
                    724:     bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
                    725:     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
                    726:     if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
                    727:         qerror_report(QERR_OPEN_FILE_FAILED, filename);
                    728:         return -1;
                    729:     }
                    730:     return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
                    731: }
1.1.1.2   root      732: 
                    733: int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
                    734: {
                    735:     const char *id = qdict_get_str(qdict, "id");
                    736:     BlockDriverState *bs;
                    737: 
                    738:     bs = bdrv_find(id);
                    739:     if (!bs) {
                    740:         qerror_report(QERR_DEVICE_NOT_FOUND, id);
                    741:         return -1;
                    742:     }
                    743:     if (bdrv_in_use(bs)) {
                    744:         qerror_report(QERR_DEVICE_IN_USE, id);
                    745:         return -1;
                    746:     }
                    747: 
                    748:     /* quiesce block driver; prevent further io */
                    749:     qemu_aio_flush();
                    750:     bdrv_flush(bs);
                    751:     bdrv_close(bs);
                    752: 
1.1.1.3   root      753:     /* if we have a device associated with this BlockDriverState (bs->peer)
                    754:      * then we need to make the drive anonymous until the device
                    755:      * can be removed.  If this is a drive with no device backing
                    756:      * then we can just get rid of the block driver state right here.
                    757:      */
1.1.1.2   root      758:     if (bs->peer) {
1.1.1.3   root      759:         bdrv_make_anon(bs);
                    760:     } else {
                    761:         drive_uninit(drive_get_by_blockdev(bs));
1.1.1.2   root      762:     }
                    763: 
                    764:     return 0;
                    765: }
                    766: 
                    767: /*
                    768:  * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
                    769:  * existing QERR_ macro mess is cleaned up.  A good example for better
                    770:  * error reports can be found in the qemu-img resize code.
                    771:  */
                    772: int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
                    773: {
                    774:     const char *device = qdict_get_str(qdict, "device");
                    775:     int64_t size = qdict_get_int(qdict, "size");
                    776:     BlockDriverState *bs;
                    777: 
                    778:     bs = bdrv_find(device);
                    779:     if (!bs) {
                    780:         qerror_report(QERR_DEVICE_NOT_FOUND, device);
                    781:         return -1;
                    782:     }
                    783: 
                    784:     if (size < 0) {
                    785:         qerror_report(QERR_UNDEFINED_ERROR);
                    786:         return -1;
                    787:     }
                    788: 
                    789:     if (bdrv_truncate(bs, size)) {
                    790:         qerror_report(QERR_UNDEFINED_ERROR);
                    791:         return -1;
                    792:     }
                    793: 
                    794:     return 0;
                    795: }

unix.superglobalmegacorp.com