Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20170921' into...
[qemu.git] / hw / block / block.c
blob27878d0087a37f3dd12bbb8474dfad30e432005f
1 /*
2 * Common code for block device models
4 * Copyright (C) 2012 Red Hat, Inc.
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 */
10 #include "qemu/osdep.h"
11 #include "sysemu/blockdev.h"
12 #include "sysemu/block-backend.h"
13 #include "hw/block/block.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
17 void blkconf_serial(BlockConf *conf, char **serial)
19 DriveInfo *dinfo;
21 if (!*serial) {
22 /* try to fall back to value set with legacy -drive serial=... */
23 dinfo = blk_legacy_dinfo(conf->blk);
24 if (dinfo) {
25 *serial = g_strdup(dinfo->serial);
30 void blkconf_blocksizes(BlockConf *conf)
32 BlockBackend *blk = conf->blk;
33 BlockSizes blocksizes;
34 int backend_ret;
36 backend_ret = blk_probe_blocksizes(blk, &blocksizes);
37 /* fill in detected values if they are not defined via qemu command line */
38 if (!conf->physical_block_size) {
39 if (!backend_ret) {
40 conf->physical_block_size = blocksizes.phys;
41 } else {
42 conf->physical_block_size = BDRV_SECTOR_SIZE;
45 if (!conf->logical_block_size) {
46 if (!backend_ret) {
47 conf->logical_block_size = blocksizes.log;
48 } else {
49 conf->logical_block_size = BDRV_SECTOR_SIZE;
54 void blkconf_apply_backend_options(BlockConf *conf, bool readonly,
55 bool resizable, Error **errp)
57 BlockBackend *blk = conf->blk;
58 BlockdevOnError rerror, werror;
59 uint64_t perm, shared_perm;
60 bool wce;
61 int ret;
63 perm = BLK_PERM_CONSISTENT_READ;
64 if (!readonly) {
65 perm |= BLK_PERM_WRITE;
68 shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
69 BLK_PERM_GRAPH_MOD;
70 if (resizable) {
71 shared_perm |= BLK_PERM_RESIZE;
73 if (conf->share_rw) {
74 shared_perm |= BLK_PERM_WRITE;
77 ret = blk_set_perm(blk, perm, shared_perm, errp);
78 if (ret < 0) {
79 return;
82 switch (conf->wce) {
83 case ON_OFF_AUTO_ON: wce = true; break;
84 case ON_OFF_AUTO_OFF: wce = false; break;
85 case ON_OFF_AUTO_AUTO: wce = blk_enable_write_cache(blk); break;
86 default:
87 abort();
90 rerror = conf->rerror;
91 if (rerror == BLOCKDEV_ON_ERROR_AUTO) {
92 rerror = blk_get_on_error(blk, true);
95 werror = conf->werror;
96 if (werror == BLOCKDEV_ON_ERROR_AUTO) {
97 werror = blk_get_on_error(blk, false);
100 blk_set_enable_write_cache(blk, wce);
101 blk_set_on_error(blk, rerror, werror);
104 void blkconf_geometry(BlockConf *conf, int *ptrans,
105 unsigned cyls_max, unsigned heads_max, unsigned secs_max,
106 Error **errp)
108 DriveInfo *dinfo;
110 if (!conf->cyls && !conf->heads && !conf->secs) {
111 /* try to fall back to value set with legacy -drive cyls=... */
112 dinfo = blk_legacy_dinfo(conf->blk);
113 if (dinfo) {
114 conf->cyls = dinfo->cyls;
115 conf->heads = dinfo->heads;
116 conf->secs = dinfo->secs;
117 if (ptrans) {
118 *ptrans = dinfo->trans;
122 if (!conf->cyls && !conf->heads && !conf->secs) {
123 hd_geometry_guess(conf->blk,
124 &conf->cyls, &conf->heads, &conf->secs,
125 ptrans);
126 } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
127 *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
129 if (conf->cyls || conf->heads || conf->secs) {
130 if (conf->cyls < 1 || conf->cyls > cyls_max) {
131 error_setg(errp, "cyls must be between 1 and %u", cyls_max);
132 return;
134 if (conf->heads < 1 || conf->heads > heads_max) {
135 error_setg(errp, "heads must be between 1 and %u", heads_max);
136 return;
138 if (conf->secs < 1 || conf->secs > secs_max) {
139 error_setg(errp, "secs must be between 1 and %u", secs_max);
140 return;