]>
Commit | Line | Data |
---|---|---|
911525db MA |
1 | /* |
2 | * Common code for block device models | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat, Inc. | |
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 | ||
80c71a24 | 10 | #include "qemu/osdep.h" |
9c17d615 | 11 | #include "sysemu/blockdev.h" |
4be74634 | 12 | #include "sysemu/block-backend.h" |
0d09e41a | 13 | #include "hw/block/block.h" |
da34e65c | 14 | #include "qapi/error.h" |
1de7afc9 | 15 | #include "qemu/error-report.h" |
911525db MA |
16 | |
17 | void blkconf_serial(BlockConf *conf, char **serial) | |
18 | { | |
19 | DriveInfo *dinfo; | |
20 | ||
21 | if (!*serial) { | |
22 | /* try to fall back to value set with legacy -drive serial=... */ | |
4be74634 | 23 | dinfo = blk_legacy_dinfo(conf->blk); |
26f8b3a8 MA |
24 | if (dinfo) { |
25 | *serial = g_strdup(dinfo->serial); | |
26 | } | |
911525db MA |
27 | } |
28 | } | |
b7eb0c9f | 29 | |
0eb28a42 ET |
30 | void blkconf_blocksizes(BlockConf *conf) |
31 | { | |
32 | BlockBackend *blk = conf->blk; | |
33 | BlockSizes blocksizes; | |
34 | int backend_ret; | |
35 | ||
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; | |
43 | } | |
44 | } | |
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; | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
9d3b1551 | 54 | bool blkconf_apply_backend_options(BlockConf *conf, bool readonly, |
a17c17a2 | 55 | bool resizable, Error **errp) |
f6166a06 KW |
56 | { |
57 | BlockBackend *blk = conf->blk; | |
8c398252 | 58 | BlockdevOnError rerror, werror; |
a17c17a2 | 59 | uint64_t perm, shared_perm; |
f6166a06 | 60 | bool wce; |
a17c17a2 KW |
61 | int ret; |
62 | ||
63 | perm = BLK_PERM_CONSISTENT_READ; | |
64 | if (!readonly) { | |
65 | perm |= BLK_PERM_WRITE; | |
66 | } | |
67 | ||
a17c17a2 | 68 | shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED | |
dabd18f6 | 69 | BLK_PERM_GRAPH_MOD; |
a17c17a2 KW |
70 | if (resizable) { |
71 | shared_perm |= BLK_PERM_RESIZE; | |
72 | } | |
dabd18f6 KW |
73 | if (conf->share_rw) { |
74 | shared_perm |= BLK_PERM_WRITE; | |
75 | } | |
a17c17a2 KW |
76 | |
77 | ret = blk_set_perm(blk, perm, shared_perm, errp); | |
78 | if (ret < 0) { | |
9d3b1551 | 79 | return false; |
a17c17a2 | 80 | } |
f6166a06 KW |
81 | |
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(); | |
88 | } | |
89 | ||
8c398252 KW |
90 | rerror = conf->rerror; |
91 | if (rerror == BLOCKDEV_ON_ERROR_AUTO) { | |
92 | rerror = blk_get_on_error(blk, true); | |
93 | } | |
94 | ||
95 | werror = conf->werror; | |
96 | if (werror == BLOCKDEV_ON_ERROR_AUTO) { | |
97 | werror = blk_get_on_error(blk, false); | |
98 | } | |
99 | ||
f6166a06 | 100 | blk_set_enable_write_cache(blk, wce); |
8c398252 | 101 | blk_set_on_error(blk, rerror, werror); |
9d3b1551 MZ |
102 | |
103 | return true; | |
f6166a06 KW |
104 | } |
105 | ||
9d3b1551 | 106 | bool blkconf_geometry(BlockConf *conf, int *ptrans, |
5ff5efb4 FZ |
107 | unsigned cyls_max, unsigned heads_max, unsigned secs_max, |
108 | Error **errp) | |
b7eb0c9f MA |
109 | { |
110 | DriveInfo *dinfo; | |
111 | ||
112 | if (!conf->cyls && !conf->heads && !conf->secs) { | |
113 | /* try to fall back to value set with legacy -drive cyls=... */ | |
4be74634 | 114 | dinfo = blk_legacy_dinfo(conf->blk); |
26f8b3a8 MA |
115 | if (dinfo) { |
116 | conf->cyls = dinfo->cyls; | |
117 | conf->heads = dinfo->heads; | |
118 | conf->secs = dinfo->secs; | |
119 | if (ptrans) { | |
120 | *ptrans = dinfo->trans; | |
121 | } | |
b7eb0c9f MA |
122 | } |
123 | } | |
124 | if (!conf->cyls && !conf->heads && !conf->secs) { | |
4be74634 | 125 | hd_geometry_guess(conf->blk, |
b7eb0c9f MA |
126 | &conf->cyls, &conf->heads, &conf->secs, |
127 | ptrans); | |
128 | } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) { | |
129 | *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs); | |
130 | } | |
131 | if (conf->cyls || conf->heads || conf->secs) { | |
132 | if (conf->cyls < 1 || conf->cyls > cyls_max) { | |
5ff5efb4 | 133 | error_setg(errp, "cyls must be between 1 and %u", cyls_max); |
9d3b1551 | 134 | return false; |
b7eb0c9f MA |
135 | } |
136 | if (conf->heads < 1 || conf->heads > heads_max) { | |
5ff5efb4 | 137 | error_setg(errp, "heads must be between 1 and %u", heads_max); |
9d3b1551 | 138 | return false; |
b7eb0c9f MA |
139 | } |
140 | if (conf->secs < 1 || conf->secs > secs_max) { | |
5ff5efb4 | 141 | error_setg(errp, "secs must be between 1 and %u", secs_max); |
9d3b1551 | 142 | return false; |
b7eb0c9f MA |
143 | } |
144 | } | |
9d3b1551 | 145 | return true; |
b7eb0c9f | 146 | } |