root / block.c @ 2ac71179
History | View | Annotate | Download (43.8 kB)
1 | fc01f7e7 | bellard | /*
|
---|---|---|---|
2 | fc01f7e7 | bellard | * QEMU System Emulator block driver
|
3 | 5fafdf24 | ths | *
|
4 | fc01f7e7 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | fc01f7e7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | fc01f7e7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | fc01f7e7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | fc01f7e7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | fc01f7e7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | fc01f7e7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | fc01f7e7 | bellard | *
|
13 | fc01f7e7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | fc01f7e7 | bellard | * all copies or substantial portions of the Software.
|
15 | fc01f7e7 | bellard | *
|
16 | fc01f7e7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | fc01f7e7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | fc01f7e7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | fc01f7e7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | fc01f7e7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | fc01f7e7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | fc01f7e7 | bellard | * THE SOFTWARE.
|
23 | fc01f7e7 | bellard | */
|
24 | 3990d09a | blueswir1 | #include "config-host.h" |
25 | 179a2c19 | blueswir1 | #ifdef HOST_BSD
|
26 | 3990d09a | blueswir1 | /* include native header before sys-queue.h */
|
27 | 3990d09a | blueswir1 | #include <sys/queue.h> |
28 | 3990d09a | blueswir1 | #endif
|
29 | 3990d09a | blueswir1 | |
30 | faf07963 | pbrook | #include "qemu-common.h" |
31 | 376253ec | aliguori | #include "monitor.h" |
32 | ea2384d3 | bellard | #include "block_int.h" |
33 | fc01f7e7 | bellard | |
34 | 179a2c19 | blueswir1 | #ifdef HOST_BSD
|
35 | 7674e7bf | bellard | #include <sys/types.h> |
36 | 7674e7bf | bellard | #include <sys/stat.h> |
37 | 7674e7bf | bellard | #include <sys/ioctl.h> |
38 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
39 | 7674e7bf | bellard | #include <sys/disk.h> |
40 | 7674e7bf | bellard | #endif
|
41 | c5e97233 | blueswir1 | #endif
|
42 | 7674e7bf | bellard | |
43 | 49dc768d | aliguori | #ifdef _WIN32
|
44 | 49dc768d | aliguori | #include <windows.h> |
45 | 49dc768d | aliguori | #endif
|
46 | 49dc768d | aliguori | |
47 | 83f64091 | bellard | #define SECTOR_BITS 9 |
48 | 83f64091 | bellard | #define SECTOR_SIZE (1 << SECTOR_BITS) |
49 | 83f64091 | bellard | |
50 | 90765429 | bellard | typedef struct BlockDriverAIOCBSync { |
51 | 90765429 | bellard | BlockDriverAIOCB common; |
52 | 90765429 | bellard | QEMUBH *bh; |
53 | 90765429 | bellard | int ret;
|
54 | f141eafe | aliguori | /* vector translation state */
|
55 | f141eafe | aliguori | QEMUIOVector *qiov; |
56 | f141eafe | aliguori | uint8_t *bounce; |
57 | f141eafe | aliguori | int is_write;
|
58 | 90765429 | bellard | } BlockDriverAIOCBSync; |
59 | 90765429 | bellard | |
60 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
61 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
62 | c87c0672 | aliguori | BlockDriverCompletionFunc *cb, void *opaque);
|
63 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
64 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
65 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
66 | 83f64091 | bellard | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
67 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
68 | 83f64091 | bellard | uint8_t *buf, int nb_sectors);
|
69 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
70 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors); |
71 | ec530c81 | bellard | |
72 | 7ee930d0 | blueswir1 | BlockDriverState *bdrv_first; |
73 | 7ee930d0 | blueswir1 | |
74 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
75 | ea2384d3 | bellard | |
76 | 83f64091 | bellard | int path_is_absolute(const char *path) |
77 | 3b0d4f61 | bellard | { |
78 | 83f64091 | bellard | const char *p; |
79 | 21664424 | bellard | #ifdef _WIN32
|
80 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
81 | 21664424 | bellard | if (*path == '/' || *path == '\\') |
82 | 21664424 | bellard | return 1; |
83 | 21664424 | bellard | #endif
|
84 | 83f64091 | bellard | p = strchr(path, ':');
|
85 | 83f64091 | bellard | if (p)
|
86 | 83f64091 | bellard | p++; |
87 | 83f64091 | bellard | else
|
88 | 83f64091 | bellard | p = path; |
89 | 3b9f94e1 | bellard | #ifdef _WIN32
|
90 | 3b9f94e1 | bellard | return (*p == '/' || *p == '\\'); |
91 | 3b9f94e1 | bellard | #else
|
92 | 3b9f94e1 | bellard | return (*p == '/'); |
93 | 3b9f94e1 | bellard | #endif
|
94 | 3b0d4f61 | bellard | } |
95 | 3b0d4f61 | bellard | |
96 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
97 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
98 | 83f64091 | bellard | supported. */
|
99 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
100 | 83f64091 | bellard | const char *base_path, |
101 | 83f64091 | bellard | const char *filename) |
102 | 3b0d4f61 | bellard | { |
103 | 83f64091 | bellard | const char *p, *p1; |
104 | 83f64091 | bellard | int len;
|
105 | 83f64091 | bellard | |
106 | 83f64091 | bellard | if (dest_size <= 0) |
107 | 83f64091 | bellard | return;
|
108 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
109 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
110 | 83f64091 | bellard | } else {
|
111 | 83f64091 | bellard | p = strchr(base_path, ':');
|
112 | 83f64091 | bellard | if (p)
|
113 | 83f64091 | bellard | p++; |
114 | 83f64091 | bellard | else
|
115 | 83f64091 | bellard | p = base_path; |
116 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
117 | 3b9f94e1 | bellard | #ifdef _WIN32
|
118 | 3b9f94e1 | bellard | { |
119 | 3b9f94e1 | bellard | const char *p2; |
120 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
121 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
122 | 3b9f94e1 | bellard | p1 = p2; |
123 | 3b9f94e1 | bellard | } |
124 | 3b9f94e1 | bellard | #endif
|
125 | 83f64091 | bellard | if (p1)
|
126 | 83f64091 | bellard | p1++; |
127 | 83f64091 | bellard | else
|
128 | 83f64091 | bellard | p1 = base_path; |
129 | 83f64091 | bellard | if (p1 > p)
|
130 | 83f64091 | bellard | p = p1; |
131 | 83f64091 | bellard | len = p - base_path; |
132 | 83f64091 | bellard | if (len > dest_size - 1) |
133 | 83f64091 | bellard | len = dest_size - 1;
|
134 | 83f64091 | bellard | memcpy(dest, base_path, len); |
135 | 83f64091 | bellard | dest[len] = '\0';
|
136 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
137 | 3b0d4f61 | bellard | } |
138 | 3b0d4f61 | bellard | } |
139 | 3b0d4f61 | bellard | |
140 | 3b0d4f61 | bellard | |
141 | 9596ebb7 | pbrook | static void bdrv_register(BlockDriver *bdrv) |
142 | ea2384d3 | bellard | { |
143 | f141eafe | aliguori | if (!bdrv->bdrv_aio_readv) {
|
144 | 83f64091 | bellard | /* add AIO emulation layer */
|
145 | f141eafe | aliguori | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
146 | f141eafe | aliguori | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
147 | 83f64091 | bellard | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; |
148 | 90765429 | bellard | bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
|
149 | eda578e5 | aliguori | } else if (!bdrv->bdrv_read) { |
150 | 83f64091 | bellard | /* add synchronous IO emulation layer */
|
151 | 83f64091 | bellard | bdrv->bdrv_read = bdrv_read_em; |
152 | 83f64091 | bellard | bdrv->bdrv_write = bdrv_write_em; |
153 | 83f64091 | bellard | } |
154 | 6bbff9a0 | aliguori | aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel); |
155 | ea2384d3 | bellard | bdrv->next = first_drv; |
156 | ea2384d3 | bellard | first_drv = bdrv; |
157 | ea2384d3 | bellard | } |
158 | b338082b | bellard | |
159 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
160 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
161 | b338082b | bellard | { |
162 | b338082b | bellard | BlockDriverState **pbs, *bs; |
163 | b338082b | bellard | |
164 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
165 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
166 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
167 | ea2384d3 | bellard | /* insert at the end */
|
168 | ea2384d3 | bellard | pbs = &bdrv_first; |
169 | ea2384d3 | bellard | while (*pbs != NULL) |
170 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
171 | ea2384d3 | bellard | *pbs = bs; |
172 | ea2384d3 | bellard | } |
173 | b338082b | bellard | return bs;
|
174 | b338082b | bellard | } |
175 | b338082b | bellard | |
176 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
177 | ea2384d3 | bellard | { |
178 | ea2384d3 | bellard | BlockDriver *drv1; |
179 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
180 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
181 | ea2384d3 | bellard | return drv1;
|
182 | ea2384d3 | bellard | } |
183 | ea2384d3 | bellard | return NULL; |
184 | ea2384d3 | bellard | } |
185 | ea2384d3 | bellard | |
186 | 5eb45639 | aliguori | int bdrv_create2(BlockDriver *drv,
|
187 | 5eb45639 | aliguori | const char *filename, int64_t size_in_sectors, |
188 | 5eb45639 | aliguori | const char *backing_file, const char *backing_format, |
189 | 5eb45639 | aliguori | int flags)
|
190 | 5eb45639 | aliguori | { |
191 | 5eb45639 | aliguori | if (drv->bdrv_create2)
|
192 | 5eb45639 | aliguori | return drv->bdrv_create2(filename, size_in_sectors, backing_file,
|
193 | 5eb45639 | aliguori | backing_format, flags); |
194 | 5eb45639 | aliguori | if (drv->bdrv_create)
|
195 | 5eb45639 | aliguori | return drv->bdrv_create(filename, size_in_sectors, backing_file,
|
196 | 5eb45639 | aliguori | flags); |
197 | 5eb45639 | aliguori | return -ENOTSUP;
|
198 | 5eb45639 | aliguori | } |
199 | 5eb45639 | aliguori | |
200 | 5fafdf24 | ths | int bdrv_create(BlockDriver *drv,
|
201 | ea2384d3 | bellard | const char *filename, int64_t size_in_sectors, |
202 | ea2384d3 | bellard | const char *backing_file, int flags) |
203 | ea2384d3 | bellard | { |
204 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
205 | ea2384d3 | bellard | return -ENOTSUP;
|
206 | ea2384d3 | bellard | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
|
207 | ea2384d3 | bellard | } |
208 | ea2384d3 | bellard | |
209 | d5249393 | bellard | #ifdef _WIN32
|
210 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
211 | d5249393 | bellard | { |
212 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
213 | 3b46e624 | ths | |
214 | 3b9f94e1 | bellard | GetTempPath(MAX_PATH, temp_dir); |
215 | 3b9f94e1 | bellard | GetTempFileName(temp_dir, "qem", 0, filename); |
216 | d5249393 | bellard | } |
217 | d5249393 | bellard | #else
|
218 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
219 | fc01f7e7 | bellard | { |
220 | 67b915a5 | bellard | int fd;
|
221 | 7ccfb2eb | blueswir1 | const char *tmpdir; |
222 | d5249393 | bellard | /* XXX: race condition possible */
|
223 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
224 | 0badc1ee | aurel32 | if (!tmpdir)
|
225 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
226 | 0badc1ee | aurel32 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
|
227 | ea2384d3 | bellard | fd = mkstemp(filename); |
228 | ea2384d3 | bellard | close(fd); |
229 | ea2384d3 | bellard | } |
230 | d5249393 | bellard | #endif
|
231 | fc01f7e7 | bellard | |
232 | 19cb3738 | bellard | #ifdef _WIN32
|
233 | f45512fe | bellard | static int is_windows_drive_prefix(const char *filename) |
234 | f45512fe | bellard | { |
235 | f45512fe | bellard | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
236 | f45512fe | bellard | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
237 | f45512fe | bellard | filename[1] == ':'); |
238 | f45512fe | bellard | } |
239 | 3b46e624 | ths | |
240 | 19cb3738 | bellard | static int is_windows_drive(const char *filename) |
241 | 19cb3738 | bellard | { |
242 | 5fafdf24 | ths | if (is_windows_drive_prefix(filename) &&
|
243 | f45512fe | bellard | filename[2] == '\0') |
244 | 19cb3738 | bellard | return 1; |
245 | 19cb3738 | bellard | if (strstart(filename, "\\\\.\\", NULL) || |
246 | 19cb3738 | bellard | strstart(filename, "//./", NULL)) |
247 | 19cb3738 | bellard | return 1; |
248 | 19cb3738 | bellard | return 0; |
249 | 19cb3738 | bellard | } |
250 | 19cb3738 | bellard | #endif
|
251 | 19cb3738 | bellard | |
252 | 83f64091 | bellard | static BlockDriver *find_protocol(const char *filename) |
253 | 83f64091 | bellard | { |
254 | 83f64091 | bellard | BlockDriver *drv1; |
255 | 83f64091 | bellard | char protocol[128]; |
256 | 83f64091 | bellard | int len;
|
257 | 83f64091 | bellard | const char *p; |
258 | 19cb3738 | bellard | |
259 | 19cb3738 | bellard | #ifdef _WIN32
|
260 | f45512fe | bellard | if (is_windows_drive(filename) ||
|
261 | f45512fe | bellard | is_windows_drive_prefix(filename)) |
262 | 19cb3738 | bellard | return &bdrv_raw;
|
263 | 19cb3738 | bellard | #endif
|
264 | 83f64091 | bellard | p = strchr(filename, ':');
|
265 | 83f64091 | bellard | if (!p)
|
266 | 83f64091 | bellard | return &bdrv_raw;
|
267 | 83f64091 | bellard | len = p - filename; |
268 | 83f64091 | bellard | if (len > sizeof(protocol) - 1) |
269 | 83f64091 | bellard | len = sizeof(protocol) - 1; |
270 | 83f64091 | bellard | memcpy(protocol, filename, len); |
271 | 83f64091 | bellard | protocol[len] = '\0';
|
272 | 83f64091 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
273 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
274 | 83f64091 | bellard | !strcmp(drv1->protocol_name, protocol)) |
275 | 83f64091 | bellard | return drv1;
|
276 | 83f64091 | bellard | } |
277 | 83f64091 | bellard | return NULL; |
278 | 83f64091 | bellard | } |
279 | 83f64091 | bellard | |
280 | 7674e7bf | bellard | /* XXX: force raw format if block or character device ? It would
|
281 | 7674e7bf | bellard | simplify the BSD case */
|
282 | ea2384d3 | bellard | static BlockDriver *find_image_format(const char *filename) |
283 | ea2384d3 | bellard | { |
284 | 83f64091 | bellard | int ret, score, score_max;
|
285 | ea2384d3 | bellard | BlockDriver *drv1, *drv; |
286 | 83f64091 | bellard | uint8_t buf[2048];
|
287 | 83f64091 | bellard | BlockDriverState *bs; |
288 | 3b46e624 | ths | |
289 | 19cb3738 | bellard | /* detect host devices. By convention, /dev/cdrom[N] is always
|
290 | 19cb3738 | bellard | recognized as a host CDROM */
|
291 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) |
292 | 19cb3738 | bellard | return &bdrv_host_device;
|
293 | 19cb3738 | bellard | #ifdef _WIN32
|
294 | 19cb3738 | bellard | if (is_windows_drive(filename))
|
295 | 19cb3738 | bellard | return &bdrv_host_device;
|
296 | 19cb3738 | bellard | #else
|
297 | 19cb3738 | bellard | { |
298 | 19cb3738 | bellard | struct stat st;
|
299 | 5fafdf24 | ths | if (stat(filename, &st) >= 0 && |
300 | 19cb3738 | bellard | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
301 | 19cb3738 | bellard | return &bdrv_host_device;
|
302 | 19cb3738 | bellard | } |
303 | 19cb3738 | bellard | } |
304 | 19cb3738 | bellard | #endif
|
305 | 3b46e624 | ths | |
306 | 83f64091 | bellard | drv = find_protocol(filename); |
307 | 19cb3738 | bellard | /* no need to test disk image formats for vvfat */
|
308 | 83f64091 | bellard | if (drv == &bdrv_vvfat)
|
309 | 83f64091 | bellard | return drv;
|
310 | 19cb3738 | bellard | |
311 | 83f64091 | bellard | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
312 | 83f64091 | bellard | if (ret < 0) |
313 | 83f64091 | bellard | return NULL; |
314 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
315 | 83f64091 | bellard | bdrv_delete(bs); |
316 | 83f64091 | bellard | if (ret < 0) { |
317 | 83f64091 | bellard | return NULL; |
318 | 83f64091 | bellard | } |
319 | 83f64091 | bellard | |
320 | ea2384d3 | bellard | score_max = 0;
|
321 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
322 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
323 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
324 | 83f64091 | bellard | if (score > score_max) {
|
325 | 83f64091 | bellard | score_max = score; |
326 | 83f64091 | bellard | drv = drv1; |
327 | 83f64091 | bellard | } |
328 | 0849bf08 | bellard | } |
329 | fc01f7e7 | bellard | } |
330 | ea2384d3 | bellard | return drv;
|
331 | ea2384d3 | bellard | } |
332 | ea2384d3 | bellard | |
333 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
334 | ea2384d3 | bellard | { |
335 | 83f64091 | bellard | BlockDriverState *bs; |
336 | 83f64091 | bellard | int ret;
|
337 | 83f64091 | bellard | |
338 | 83f64091 | bellard | bs = bdrv_new("");
|
339 | 83f64091 | bellard | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
|
340 | 83f64091 | bellard | if (ret < 0) { |
341 | 83f64091 | bellard | bdrv_delete(bs); |
342 | 83f64091 | bellard | return ret;
|
343 | 3b0d4f61 | bellard | } |
344 | 71d0770c | aliguori | bs->growable = 1;
|
345 | 83f64091 | bellard | *pbs = bs; |
346 | 83f64091 | bellard | return 0; |
347 | 83f64091 | bellard | } |
348 | 83f64091 | bellard | |
349 | 83f64091 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
350 | 83f64091 | bellard | { |
351 | 83f64091 | bellard | return bdrv_open2(bs, filename, flags, NULL); |
352 | ea2384d3 | bellard | } |
353 | ea2384d3 | bellard | |
354 | 83f64091 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
355 | ea2384d3 | bellard | BlockDriver *drv) |
356 | ea2384d3 | bellard | { |
357 | 83f64091 | bellard | int ret, open_flags;
|
358 | eb5c851f | ths | char tmp_filename[PATH_MAX];
|
359 | eb5c851f | ths | char backing_filename[PATH_MAX];
|
360 | 3b46e624 | ths | |
361 | ea2384d3 | bellard | bs->read_only = 0;
|
362 | ea2384d3 | bellard | bs->is_temporary = 0;
|
363 | ea2384d3 | bellard | bs->encrypted = 0;
|
364 | c0f4ce77 | aliguori | bs->valid_key = 0;
|
365 | e268ca52 | aliguori | /* buffer_alignment defaulted to 512, drivers can change this value */
|
366 | e268ca52 | aliguori | bs->buffer_alignment = 512;
|
367 | 712e7874 | bellard | |
368 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
369 | ea2384d3 | bellard | BlockDriverState *bs1; |
370 | ea2384d3 | bellard | int64_t total_size; |
371 | 7c96d46e | aliguori | int is_protocol = 0; |
372 | 3b46e624 | ths | |
373 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
374 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
375 | 33e3963e | bellard | |
376 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
377 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
378 | 5eb45639 | aliguori | ret = bdrv_open2(bs1, filename, 0, drv);
|
379 | 51d7c00c | aliguori | if (ret < 0) { |
380 | ea2384d3 | bellard | bdrv_delete(bs1); |
381 | 51d7c00c | aliguori | return ret;
|
382 | ea2384d3 | bellard | } |
383 | 83f64091 | bellard | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
384 | 7c96d46e | aliguori | |
385 | 7c96d46e | aliguori | if (bs1->drv && bs1->drv->protocol_name)
|
386 | 7c96d46e | aliguori | is_protocol = 1;
|
387 | 7c96d46e | aliguori | |
388 | ea2384d3 | bellard | bdrv_delete(bs1); |
389 | 3b46e624 | ths | |
390 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
391 | 7c96d46e | aliguori | |
392 | 7c96d46e | aliguori | /* Real path is meaningless for protocols */
|
393 | 7c96d46e | aliguori | if (is_protocol)
|
394 | 7c96d46e | aliguori | snprintf(backing_filename, sizeof(backing_filename),
|
395 | 7c96d46e | aliguori | "%s", filename);
|
396 | 7c96d46e | aliguori | else
|
397 | 7c96d46e | aliguori | realpath(filename, backing_filename); |
398 | 7c96d46e | aliguori | |
399 | 5eb45639 | aliguori | ret = bdrv_create2(&bdrv_qcow2, tmp_filename, |
400 | 5eb45639 | aliguori | total_size, backing_filename, |
401 | 5eb45639 | aliguori | (drv ? drv->format_name : NULL), 0); |
402 | 51d7c00c | aliguori | if (ret < 0) { |
403 | 51d7c00c | aliguori | return ret;
|
404 | ea2384d3 | bellard | } |
405 | ea2384d3 | bellard | filename = tmp_filename; |
406 | 5eb45639 | aliguori | drv = &bdrv_qcow2; |
407 | ea2384d3 | bellard | bs->is_temporary = 1;
|
408 | ea2384d3 | bellard | } |
409 | 712e7874 | bellard | |
410 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
411 | 83f64091 | bellard | if (flags & BDRV_O_FILE) {
|
412 | 83f64091 | bellard | drv = find_protocol(filename); |
413 | 51d7c00c | aliguori | } else if (!drv) { |
414 | 51d7c00c | aliguori | drv = find_image_format(filename); |
415 | 51d7c00c | aliguori | } |
416 | 51d7c00c | aliguori | if (!drv) {
|
417 | 51d7c00c | aliguori | ret = -ENOENT; |
418 | 51d7c00c | aliguori | goto unlink_and_fail;
|
419 | ea2384d3 | bellard | } |
420 | ea2384d3 | bellard | bs->drv = drv; |
421 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
422 | 83f64091 | bellard | /* Note: for compatibility, we open disk image files as RDWR, and
|
423 | 83f64091 | bellard | RDONLY as fallback */
|
424 | 83f64091 | bellard | if (!(flags & BDRV_O_FILE))
|
425 | 9f7965c7 | aliguori | open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); |
426 | 83f64091 | bellard | else
|
427 | 83f64091 | bellard | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
428 | 83f64091 | bellard | ret = drv->bdrv_open(bs, filename, open_flags); |
429 | a0a83536 | aurel32 | if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
|
430 | 9f7965c7 | aliguori | ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); |
431 | 83f64091 | bellard | bs->read_only = 1;
|
432 | 83f64091 | bellard | } |
433 | ea2384d3 | bellard | if (ret < 0) { |
434 | ea2384d3 | bellard | qemu_free(bs->opaque); |
435 | 6b21b973 | bellard | bs->opaque = NULL;
|
436 | 6b21b973 | bellard | bs->drv = NULL;
|
437 | 51d7c00c | aliguori | unlink_and_fail:
|
438 | 51d7c00c | aliguori | if (bs->is_temporary)
|
439 | 51d7c00c | aliguori | unlink(filename); |
440 | 83f64091 | bellard | return ret;
|
441 | 33e3963e | bellard | } |
442 | d15a771d | bellard | if (drv->bdrv_getlength) {
|
443 | d15a771d | bellard | bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
444 | d15a771d | bellard | } |
445 | 67b915a5 | bellard | #ifndef _WIN32
|
446 | ea2384d3 | bellard | if (bs->is_temporary) {
|
447 | ea2384d3 | bellard | unlink(filename); |
448 | ea2384d3 | bellard | } |
449 | ea2384d3 | bellard | #endif
|
450 | 83f64091 | bellard | if (bs->backing_file[0] != '\0') { |
451 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
452 | 5eb45639 | aliguori | BlockDriver *back_drv = NULL;
|
453 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
454 | 83f64091 | bellard | path_combine(backing_filename, sizeof(backing_filename),
|
455 | 83f64091 | bellard | filename, bs->backing_file); |
456 | 5eb45639 | aliguori | if (bs->backing_format[0] != '\0') |
457 | 5eb45639 | aliguori | back_drv = bdrv_find_format(bs->backing_format); |
458 | 5eb45639 | aliguori | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, |
459 | 5eb45639 | aliguori | back_drv); |
460 | 51d7c00c | aliguori | if (ret < 0) { |
461 | 51d7c00c | aliguori | bdrv_close(bs); |
462 | 51d7c00c | aliguori | return ret;
|
463 | 51d7c00c | aliguori | } |
464 | 33e3963e | bellard | } |
465 | 33e3963e | bellard | |
466 | bb5fc20f | aliguori | if (!bdrv_key_required(bs)) {
|
467 | bb5fc20f | aliguori | /* call the change callback */
|
468 | bb5fc20f | aliguori | bs->media_changed = 1;
|
469 | bb5fc20f | aliguori | if (bs->change_cb)
|
470 | bb5fc20f | aliguori | bs->change_cb(bs->change_opaque); |
471 | bb5fc20f | aliguori | } |
472 | b338082b | bellard | return 0; |
473 | fc01f7e7 | bellard | } |
474 | fc01f7e7 | bellard | |
475 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
476 | fc01f7e7 | bellard | { |
477 | 19cb3738 | bellard | if (bs->drv) {
|
478 | ea2384d3 | bellard | if (bs->backing_hd)
|
479 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
480 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
481 | ea2384d3 | bellard | qemu_free(bs->opaque); |
482 | ea2384d3 | bellard | #ifdef _WIN32
|
483 | ea2384d3 | bellard | if (bs->is_temporary) {
|
484 | ea2384d3 | bellard | unlink(bs->filename); |
485 | ea2384d3 | bellard | } |
486 | 67b915a5 | bellard | #endif
|
487 | ea2384d3 | bellard | bs->opaque = NULL;
|
488 | ea2384d3 | bellard | bs->drv = NULL;
|
489 | b338082b | bellard | |
490 | b338082b | bellard | /* call the change callback */
|
491 | 19cb3738 | bellard | bs->media_changed = 1;
|
492 | b338082b | bellard | if (bs->change_cb)
|
493 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
494 | b338082b | bellard | } |
495 | b338082b | bellard | } |
496 | b338082b | bellard | |
497 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
498 | b338082b | bellard | { |
499 | 34c6f050 | aurel32 | BlockDriverState **pbs; |
500 | 34c6f050 | aurel32 | |
501 | 34c6f050 | aurel32 | pbs = &bdrv_first; |
502 | 34c6f050 | aurel32 | while (*pbs != bs && *pbs != NULL) |
503 | 34c6f050 | aurel32 | pbs = &(*pbs)->next; |
504 | 34c6f050 | aurel32 | if (*pbs == bs)
|
505 | 34c6f050 | aurel32 | *pbs = bs->next; |
506 | 34c6f050 | aurel32 | |
507 | b338082b | bellard | bdrv_close(bs); |
508 | b338082b | bellard | qemu_free(bs); |
509 | fc01f7e7 | bellard | } |
510 | fc01f7e7 | bellard | |
511 | e97fc193 | aliguori | /*
|
512 | e97fc193 | aliguori | * Run consistency checks on an image
|
513 | e97fc193 | aliguori | *
|
514 | e97fc193 | aliguori | * Returns the number of errors or -errno when an internal error occurs
|
515 | e97fc193 | aliguori | */
|
516 | e97fc193 | aliguori | int bdrv_check(BlockDriverState *bs)
|
517 | e97fc193 | aliguori | { |
518 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
519 | e97fc193 | aliguori | return -ENOTSUP;
|
520 | e97fc193 | aliguori | } |
521 | e97fc193 | aliguori | |
522 | e97fc193 | aliguori | return bs->drv->bdrv_check(bs);
|
523 | e97fc193 | aliguori | } |
524 | e97fc193 | aliguori | |
525 | 33e3963e | bellard | /* commit COW file into the raw image */
|
526 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
527 | 33e3963e | bellard | { |
528 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
529 | 83f64091 | bellard | int64_t i, total_sectors; |
530 | ea2384d3 | bellard | int n, j;
|
531 | ea2384d3 | bellard | unsigned char sector[512]; |
532 | 33e3963e | bellard | |
533 | 19cb3738 | bellard | if (!drv)
|
534 | 19cb3738 | bellard | return -ENOMEDIUM;
|
535 | 33e3963e | bellard | |
536 | 33e3963e | bellard | if (bs->read_only) {
|
537 | ea2384d3 | bellard | return -EACCES;
|
538 | 33e3963e | bellard | } |
539 | 33e3963e | bellard | |
540 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
541 | ea2384d3 | bellard | return -ENOTSUP;
|
542 | ea2384d3 | bellard | } |
543 | 33e3963e | bellard | |
544 | 83f64091 | bellard | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
545 | 83f64091 | bellard | for (i = 0; i < total_sectors;) { |
546 | 19cb3738 | bellard | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
547 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
548 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
549 | ea2384d3 | bellard | return -EIO;
|
550 | ea2384d3 | bellard | } |
551 | ea2384d3 | bellard | |
552 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
553 | ea2384d3 | bellard | return -EIO;
|
554 | ea2384d3 | bellard | } |
555 | ea2384d3 | bellard | i++; |
556 | 33e3963e | bellard | } |
557 | ea2384d3 | bellard | } else {
|
558 | ea2384d3 | bellard | i += n; |
559 | ea2384d3 | bellard | } |
560 | 33e3963e | bellard | } |
561 | 95389c86 | bellard | |
562 | 19cb3738 | bellard | if (drv->bdrv_make_empty)
|
563 | 19cb3738 | bellard | return drv->bdrv_make_empty(bs);
|
564 | 95389c86 | bellard | |
565 | 33e3963e | bellard | return 0; |
566 | 33e3963e | bellard | } |
567 | 33e3963e | bellard | |
568 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
569 | 71d0770c | aliguori | size_t size) |
570 | 71d0770c | aliguori | { |
571 | 71d0770c | aliguori | int64_t len; |
572 | 71d0770c | aliguori | |
573 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
574 | 71d0770c | aliguori | return -ENOMEDIUM;
|
575 | 71d0770c | aliguori | |
576 | 71d0770c | aliguori | if (bs->growable)
|
577 | 71d0770c | aliguori | return 0; |
578 | 71d0770c | aliguori | |
579 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
580 | 71d0770c | aliguori | |
581 | 71d0770c | aliguori | if ((offset + size) > len)
|
582 | 71d0770c | aliguori | return -EIO;
|
583 | 71d0770c | aliguori | |
584 | 71d0770c | aliguori | return 0; |
585 | 71d0770c | aliguori | } |
586 | 71d0770c | aliguori | |
587 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
588 | 71d0770c | aliguori | int nb_sectors)
|
589 | 71d0770c | aliguori | { |
590 | 999dec57 | aliguori | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
591 | 71d0770c | aliguori | } |
592 | 71d0770c | aliguori | |
593 | 19cb3738 | bellard | /* return < 0 if error. See bdrv_write() for the return codes */
|
594 | 5fafdf24 | ths | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
595 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
596 | fc01f7e7 | bellard | { |
597 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
598 | ea2384d3 | bellard | |
599 | 19cb3738 | bellard | if (!drv)
|
600 | 19cb3738 | bellard | return -ENOMEDIUM;
|
601 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
602 | 71d0770c | aliguori | return -EIO;
|
603 | b338082b | bellard | |
604 | eda578e5 | aliguori | return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
|
605 | fc01f7e7 | bellard | } |
606 | fc01f7e7 | bellard | |
607 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
608 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
609 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
610 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
611 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
612 | 19cb3738 | bellard | */
|
613 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
614 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
615 | fc01f7e7 | bellard | { |
616 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
617 | 19cb3738 | bellard | if (!bs->drv)
|
618 | 19cb3738 | bellard | return -ENOMEDIUM;
|
619 | 0849bf08 | bellard | if (bs->read_only)
|
620 | 19cb3738 | bellard | return -EACCES;
|
621 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
622 | 71d0770c | aliguori | return -EIO;
|
623 | 71d0770c | aliguori | |
624 | 42fb2807 | aliguori | return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
625 | 83f64091 | bellard | } |
626 | 83f64091 | bellard | |
627 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
628 | eda578e5 | aliguori | void *buf, int count1) |
629 | 83f64091 | bellard | { |
630 | 83f64091 | bellard | uint8_t tmp_buf[SECTOR_SIZE]; |
631 | 83f64091 | bellard | int len, nb_sectors, count;
|
632 | 83f64091 | bellard | int64_t sector_num; |
633 | 83f64091 | bellard | |
634 | 83f64091 | bellard | count = count1; |
635 | 83f64091 | bellard | /* first read to align to sector start */
|
636 | 83f64091 | bellard | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
|
637 | 83f64091 | bellard | if (len > count)
|
638 | 83f64091 | bellard | len = count; |
639 | 83f64091 | bellard | sector_num = offset >> SECTOR_BITS; |
640 | 83f64091 | bellard | if (len > 0) { |
641 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
642 | 83f64091 | bellard | return -EIO;
|
643 | 83f64091 | bellard | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
|
644 | 83f64091 | bellard | count -= len; |
645 | 83f64091 | bellard | if (count == 0) |
646 | 83f64091 | bellard | return count1;
|
647 | 83f64091 | bellard | sector_num++; |
648 | 83f64091 | bellard | buf += len; |
649 | 83f64091 | bellard | } |
650 | 83f64091 | bellard | |
651 | 83f64091 | bellard | /* read the sectors "in place" */
|
652 | 83f64091 | bellard | nb_sectors = count >> SECTOR_BITS; |
653 | 83f64091 | bellard | if (nb_sectors > 0) { |
654 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) |
655 | 83f64091 | bellard | return -EIO;
|
656 | 83f64091 | bellard | sector_num += nb_sectors; |
657 | 83f64091 | bellard | len = nb_sectors << SECTOR_BITS; |
658 | 83f64091 | bellard | buf += len; |
659 | 83f64091 | bellard | count -= len; |
660 | 83f64091 | bellard | } |
661 | 83f64091 | bellard | |
662 | 83f64091 | bellard | /* add data from the last sector */
|
663 | 83f64091 | bellard | if (count > 0) { |
664 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
665 | 83f64091 | bellard | return -EIO;
|
666 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
667 | 83f64091 | bellard | } |
668 | 83f64091 | bellard | return count1;
|
669 | 83f64091 | bellard | } |
670 | 83f64091 | bellard | |
671 | eda578e5 | aliguori | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
672 | eda578e5 | aliguori | const void *buf, int count1) |
673 | 83f64091 | bellard | { |
674 | 83f64091 | bellard | uint8_t tmp_buf[SECTOR_SIZE]; |
675 | 83f64091 | bellard | int len, nb_sectors, count;
|
676 | 83f64091 | bellard | int64_t sector_num; |
677 | 83f64091 | bellard | |
678 | 83f64091 | bellard | count = count1; |
679 | 83f64091 | bellard | /* first write to align to sector start */
|
680 | 83f64091 | bellard | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
|
681 | 83f64091 | bellard | if (len > count)
|
682 | 83f64091 | bellard | len = count; |
683 | 83f64091 | bellard | sector_num = offset >> SECTOR_BITS; |
684 | 83f64091 | bellard | if (len > 0) { |
685 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
686 | 83f64091 | bellard | return -EIO;
|
687 | 83f64091 | bellard | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
|
688 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
689 | 83f64091 | bellard | return -EIO;
|
690 | 83f64091 | bellard | count -= len; |
691 | 83f64091 | bellard | if (count == 0) |
692 | 83f64091 | bellard | return count1;
|
693 | 83f64091 | bellard | sector_num++; |
694 | 83f64091 | bellard | buf += len; |
695 | 83f64091 | bellard | } |
696 | 83f64091 | bellard | |
697 | 83f64091 | bellard | /* write the sectors "in place" */
|
698 | 83f64091 | bellard | nb_sectors = count >> SECTOR_BITS; |
699 | 83f64091 | bellard | if (nb_sectors > 0) { |
700 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) |
701 | 83f64091 | bellard | return -EIO;
|
702 | 83f64091 | bellard | sector_num += nb_sectors; |
703 | 83f64091 | bellard | len = nb_sectors << SECTOR_BITS; |
704 | 83f64091 | bellard | buf += len; |
705 | 83f64091 | bellard | count -= len; |
706 | 83f64091 | bellard | } |
707 | 83f64091 | bellard | |
708 | 83f64091 | bellard | /* add data from the last sector */
|
709 | 83f64091 | bellard | if (count > 0) { |
710 | 83f64091 | bellard | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) |
711 | 83f64091 | bellard | return -EIO;
|
712 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
713 | 83f64091 | bellard | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) |
714 | 83f64091 | bellard | return -EIO;
|
715 | 83f64091 | bellard | } |
716 | 83f64091 | bellard | return count1;
|
717 | 83f64091 | bellard | } |
718 | 83f64091 | bellard | |
719 | 83f64091 | bellard | /**
|
720 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
721 | 83f64091 | bellard | */
|
722 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
723 | 83f64091 | bellard | { |
724 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
725 | 83f64091 | bellard | if (!drv)
|
726 | 19cb3738 | bellard | return -ENOMEDIUM;
|
727 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
728 | 83f64091 | bellard | return -ENOTSUP;
|
729 | 83f64091 | bellard | return drv->bdrv_truncate(bs, offset);
|
730 | 83f64091 | bellard | } |
731 | 83f64091 | bellard | |
732 | 83f64091 | bellard | /**
|
733 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
734 | 83f64091 | bellard | */
|
735 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
736 | 83f64091 | bellard | { |
737 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
738 | 83f64091 | bellard | if (!drv)
|
739 | 19cb3738 | bellard | return -ENOMEDIUM;
|
740 | 83f64091 | bellard | if (!drv->bdrv_getlength) {
|
741 | 83f64091 | bellard | /* legacy mode */
|
742 | 83f64091 | bellard | return bs->total_sectors * SECTOR_SIZE;
|
743 | 83f64091 | bellard | } |
744 | 83f64091 | bellard | return drv->bdrv_getlength(bs);
|
745 | fc01f7e7 | bellard | } |
746 | fc01f7e7 | bellard | |
747 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
748 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
749 | fc01f7e7 | bellard | { |
750 | 19cb3738 | bellard | int64_t length; |
751 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
752 | 19cb3738 | bellard | if (length < 0) |
753 | 19cb3738 | bellard | length = 0;
|
754 | 19cb3738 | bellard | else
|
755 | 19cb3738 | bellard | length = length >> SECTOR_BITS; |
756 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
757 | fc01f7e7 | bellard | } |
758 | cf98951b | bellard | |
759 | f3d54fc4 | aliguori | struct partition {
|
760 | f3d54fc4 | aliguori | uint8_t boot_ind; /* 0x80 - active */
|
761 | f3d54fc4 | aliguori | uint8_t head; /* starting head */
|
762 | f3d54fc4 | aliguori | uint8_t sector; /* starting sector */
|
763 | f3d54fc4 | aliguori | uint8_t cyl; /* starting cylinder */
|
764 | f3d54fc4 | aliguori | uint8_t sys_ind; /* What partition type */
|
765 | f3d54fc4 | aliguori | uint8_t end_head; /* end head */
|
766 | f3d54fc4 | aliguori | uint8_t end_sector; /* end sector */
|
767 | f3d54fc4 | aliguori | uint8_t end_cyl; /* end cylinder */
|
768 | f3d54fc4 | aliguori | uint32_t start_sect; /* starting sector counting from 0 */
|
769 | f3d54fc4 | aliguori | uint32_t nr_sects; /* nr of sectors in partition */
|
770 | f3d54fc4 | aliguori | } __attribute__((packed)); |
771 | f3d54fc4 | aliguori | |
772 | f3d54fc4 | aliguori | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
|
773 | f3d54fc4 | aliguori | static int guess_disk_lchs(BlockDriverState *bs, |
774 | f3d54fc4 | aliguori | int *pcylinders, int *pheads, int *psectors) |
775 | f3d54fc4 | aliguori | { |
776 | f3d54fc4 | aliguori | uint8_t buf[512];
|
777 | f3d54fc4 | aliguori | int ret, i, heads, sectors, cylinders;
|
778 | f3d54fc4 | aliguori | struct partition *p;
|
779 | f3d54fc4 | aliguori | uint32_t nr_sects; |
780 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
781 | f3d54fc4 | aliguori | |
782 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
783 | f3d54fc4 | aliguori | |
784 | f3d54fc4 | aliguori | ret = bdrv_read(bs, 0, buf, 1); |
785 | f3d54fc4 | aliguori | if (ret < 0) |
786 | f3d54fc4 | aliguori | return -1; |
787 | f3d54fc4 | aliguori | /* test msdos magic */
|
788 | f3d54fc4 | aliguori | if (buf[510] != 0x55 || buf[511] != 0xaa) |
789 | f3d54fc4 | aliguori | return -1; |
790 | f3d54fc4 | aliguori | for(i = 0; i < 4; i++) { |
791 | f3d54fc4 | aliguori | p = ((struct partition *)(buf + 0x1be)) + i; |
792 | f3d54fc4 | aliguori | nr_sects = le32_to_cpu(p->nr_sects); |
793 | f3d54fc4 | aliguori | if (nr_sects && p->end_head) {
|
794 | f3d54fc4 | aliguori | /* We make the assumption that the partition terminates on
|
795 | f3d54fc4 | aliguori | a cylinder boundary */
|
796 | f3d54fc4 | aliguori | heads = p->end_head + 1;
|
797 | f3d54fc4 | aliguori | sectors = p->end_sector & 63;
|
798 | f3d54fc4 | aliguori | if (sectors == 0) |
799 | f3d54fc4 | aliguori | continue;
|
800 | f3d54fc4 | aliguori | cylinders = nb_sectors / (heads * sectors); |
801 | f3d54fc4 | aliguori | if (cylinders < 1 || cylinders > 16383) |
802 | f3d54fc4 | aliguori | continue;
|
803 | f3d54fc4 | aliguori | *pheads = heads; |
804 | f3d54fc4 | aliguori | *psectors = sectors; |
805 | f3d54fc4 | aliguori | *pcylinders = cylinders; |
806 | f3d54fc4 | aliguori | #if 0
|
807 | f3d54fc4 | aliguori | printf("guessed geometry: LCHS=%d %d %d\n",
|
808 | f3d54fc4 | aliguori | cylinders, heads, sectors);
|
809 | f3d54fc4 | aliguori | #endif
|
810 | f3d54fc4 | aliguori | return 0; |
811 | f3d54fc4 | aliguori | } |
812 | f3d54fc4 | aliguori | } |
813 | f3d54fc4 | aliguori | return -1; |
814 | f3d54fc4 | aliguori | } |
815 | f3d54fc4 | aliguori | |
816 | f3d54fc4 | aliguori | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
817 | f3d54fc4 | aliguori | { |
818 | f3d54fc4 | aliguori | int translation, lba_detected = 0; |
819 | f3d54fc4 | aliguori | int cylinders, heads, secs;
|
820 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
821 | f3d54fc4 | aliguori | |
822 | f3d54fc4 | aliguori | /* if a geometry hint is available, use it */
|
823 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
824 | f3d54fc4 | aliguori | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
825 | f3d54fc4 | aliguori | translation = bdrv_get_translation_hint(bs); |
826 | f3d54fc4 | aliguori | if (cylinders != 0) { |
827 | f3d54fc4 | aliguori | *pcyls = cylinders; |
828 | f3d54fc4 | aliguori | *pheads = heads; |
829 | f3d54fc4 | aliguori | *psecs = secs; |
830 | f3d54fc4 | aliguori | } else {
|
831 | f3d54fc4 | aliguori | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
832 | f3d54fc4 | aliguori | if (heads > 16) { |
833 | f3d54fc4 | aliguori | /* if heads > 16, it means that a BIOS LBA
|
834 | f3d54fc4 | aliguori | translation was active, so the default
|
835 | f3d54fc4 | aliguori | hardware geometry is OK */
|
836 | f3d54fc4 | aliguori | lba_detected = 1;
|
837 | f3d54fc4 | aliguori | goto default_geometry;
|
838 | f3d54fc4 | aliguori | } else {
|
839 | f3d54fc4 | aliguori | *pcyls = cylinders; |
840 | f3d54fc4 | aliguori | *pheads = heads; |
841 | f3d54fc4 | aliguori | *psecs = secs; |
842 | f3d54fc4 | aliguori | /* disable any translation to be in sync with
|
843 | f3d54fc4 | aliguori | the logical geometry */
|
844 | f3d54fc4 | aliguori | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
845 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
846 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_NONE); |
847 | f3d54fc4 | aliguori | } |
848 | f3d54fc4 | aliguori | } |
849 | f3d54fc4 | aliguori | } else {
|
850 | f3d54fc4 | aliguori | default_geometry:
|
851 | f3d54fc4 | aliguori | /* if no geometry, use a standard physical disk geometry */
|
852 | f3d54fc4 | aliguori | cylinders = nb_sectors / (16 * 63); |
853 | f3d54fc4 | aliguori | |
854 | f3d54fc4 | aliguori | if (cylinders > 16383) |
855 | f3d54fc4 | aliguori | cylinders = 16383;
|
856 | f3d54fc4 | aliguori | else if (cylinders < 2) |
857 | f3d54fc4 | aliguori | cylinders = 2;
|
858 | f3d54fc4 | aliguori | *pcyls = cylinders; |
859 | f3d54fc4 | aliguori | *pheads = 16;
|
860 | f3d54fc4 | aliguori | *psecs = 63;
|
861 | f3d54fc4 | aliguori | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
862 | f3d54fc4 | aliguori | if ((*pcyls * *pheads) <= 131072) { |
863 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
864 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LARGE); |
865 | f3d54fc4 | aliguori | } else {
|
866 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
867 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LBA); |
868 | f3d54fc4 | aliguori | } |
869 | f3d54fc4 | aliguori | } |
870 | f3d54fc4 | aliguori | } |
871 | f3d54fc4 | aliguori | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
872 | f3d54fc4 | aliguori | } |
873 | f3d54fc4 | aliguori | } |
874 | f3d54fc4 | aliguori | |
875 | 5fafdf24 | ths | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
876 | b338082b | bellard | int cyls, int heads, int secs) |
877 | b338082b | bellard | { |
878 | b338082b | bellard | bs->cyls = cyls; |
879 | b338082b | bellard | bs->heads = heads; |
880 | b338082b | bellard | bs->secs = secs; |
881 | b338082b | bellard | } |
882 | b338082b | bellard | |
883 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
884 | b338082b | bellard | { |
885 | b338082b | bellard | bs->type = type; |
886 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
887 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
888 | b338082b | bellard | } |
889 | b338082b | bellard | |
890 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
891 | 46d4767d | bellard | { |
892 | 46d4767d | bellard | bs->translation = translation; |
893 | 46d4767d | bellard | } |
894 | 46d4767d | bellard | |
895 | 5fafdf24 | ths | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
896 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
897 | b338082b | bellard | { |
898 | b338082b | bellard | *pcyls = bs->cyls; |
899 | b338082b | bellard | *pheads = bs->heads; |
900 | b338082b | bellard | *psecs = bs->secs; |
901 | b338082b | bellard | } |
902 | b338082b | bellard | |
903 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
904 | b338082b | bellard | { |
905 | b338082b | bellard | return bs->type;
|
906 | b338082b | bellard | } |
907 | b338082b | bellard | |
908 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
909 | 46d4767d | bellard | { |
910 | 46d4767d | bellard | return bs->translation;
|
911 | 46d4767d | bellard | } |
912 | 46d4767d | bellard | |
913 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
914 | b338082b | bellard | { |
915 | b338082b | bellard | return bs->removable;
|
916 | b338082b | bellard | } |
917 | b338082b | bellard | |
918 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
919 | b338082b | bellard | { |
920 | b338082b | bellard | return bs->read_only;
|
921 | b338082b | bellard | } |
922 | b338082b | bellard | |
923 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
924 | 985a03b0 | ths | { |
925 | 985a03b0 | ths | return bs->sg;
|
926 | 985a03b0 | ths | } |
927 | 985a03b0 | ths | |
928 | 19cb3738 | bellard | /* XXX: no longer used */
|
929 | 5fafdf24 | ths | void bdrv_set_change_cb(BlockDriverState *bs,
|
930 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
931 | b338082b | bellard | { |
932 | b338082b | bellard | bs->change_cb = change_cb; |
933 | b338082b | bellard | bs->change_opaque = opaque; |
934 | b338082b | bellard | } |
935 | b338082b | bellard | |
936 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
937 | ea2384d3 | bellard | { |
938 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
939 | ea2384d3 | bellard | return 1; |
940 | ea2384d3 | bellard | return bs->encrypted;
|
941 | ea2384d3 | bellard | } |
942 | ea2384d3 | bellard | |
943 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
944 | c0f4ce77 | aliguori | { |
945 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
946 | c0f4ce77 | aliguori | |
947 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
948 | c0f4ce77 | aliguori | return 1; |
949 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
950 | c0f4ce77 | aliguori | } |
951 | c0f4ce77 | aliguori | |
952 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
953 | ea2384d3 | bellard | { |
954 | ea2384d3 | bellard | int ret;
|
955 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
956 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
957 | ea2384d3 | bellard | if (ret < 0) |
958 | ea2384d3 | bellard | return ret;
|
959 | ea2384d3 | bellard | if (!bs->encrypted)
|
960 | ea2384d3 | bellard | return 0; |
961 | ea2384d3 | bellard | } |
962 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
963 | ea2384d3 | bellard | return -1; |
964 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
965 | bb5fc20f | aliguori | if (ret < 0) { |
966 | bb5fc20f | aliguori | bs->valid_key = 0;
|
967 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
968 | bb5fc20f | aliguori | bs->valid_key = 1;
|
969 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
970 | bb5fc20f | aliguori | bs->media_changed = 1;
|
971 | bb5fc20f | aliguori | if (bs->change_cb)
|
972 | bb5fc20f | aliguori | bs->change_cb(bs->change_opaque); |
973 | bb5fc20f | aliguori | } |
974 | c0f4ce77 | aliguori | return ret;
|
975 | ea2384d3 | bellard | } |
976 | ea2384d3 | bellard | |
977 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
978 | ea2384d3 | bellard | { |
979 | 19cb3738 | bellard | if (!bs->drv) {
|
980 | ea2384d3 | bellard | buf[0] = '\0'; |
981 | ea2384d3 | bellard | } else {
|
982 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
983 | ea2384d3 | bellard | } |
984 | ea2384d3 | bellard | } |
985 | ea2384d3 | bellard | |
986 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
987 | ea2384d3 | bellard | void *opaque)
|
988 | ea2384d3 | bellard | { |
989 | ea2384d3 | bellard | BlockDriver *drv; |
990 | ea2384d3 | bellard | |
991 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
992 | ea2384d3 | bellard | it(opaque, drv->format_name); |
993 | ea2384d3 | bellard | } |
994 | ea2384d3 | bellard | } |
995 | ea2384d3 | bellard | |
996 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
997 | b338082b | bellard | { |
998 | b338082b | bellard | BlockDriverState *bs; |
999 | b338082b | bellard | |
1000 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1001 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
1002 | b338082b | bellard | return bs;
|
1003 | b338082b | bellard | } |
1004 | b338082b | bellard | return NULL; |
1005 | b338082b | bellard | } |
1006 | b338082b | bellard | |
1007 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
1008 | 81d0912d | bellard | { |
1009 | 81d0912d | bellard | BlockDriverState *bs; |
1010 | 81d0912d | bellard | |
1011 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1012 | 51de9760 | aliguori | it(opaque, bs); |
1013 | 81d0912d | bellard | } |
1014 | 81d0912d | bellard | } |
1015 | 81d0912d | bellard | |
1016 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
1017 | ea2384d3 | bellard | { |
1018 | ea2384d3 | bellard | return bs->device_name;
|
1019 | ea2384d3 | bellard | } |
1020 | ea2384d3 | bellard | |
1021 | 7a6cba61 | pbrook | void bdrv_flush(BlockDriverState *bs)
|
1022 | 7a6cba61 | pbrook | { |
1023 | 081501da | aliguori | if (!bs->drv)
|
1024 | 081501da | aliguori | return;
|
1025 | 7a6cba61 | pbrook | if (bs->drv->bdrv_flush)
|
1026 | 7a6cba61 | pbrook | bs->drv->bdrv_flush(bs); |
1027 | 7a6cba61 | pbrook | if (bs->backing_hd)
|
1028 | 7a6cba61 | pbrook | bdrv_flush(bs->backing_hd); |
1029 | 7a6cba61 | pbrook | } |
1030 | 7a6cba61 | pbrook | |
1031 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
1032 | c6ca28d6 | aliguori | { |
1033 | c6ca28d6 | aliguori | BlockDriverState *bs; |
1034 | c6ca28d6 | aliguori | |
1035 | c6ca28d6 | aliguori | for (bs = bdrv_first; bs != NULL; bs = bs->next) |
1036 | c6ca28d6 | aliguori | if (bs->drv && !bdrv_is_read_only(bs) &&
|
1037 | c6ca28d6 | aliguori | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) |
1038 | c6ca28d6 | aliguori | bdrv_flush(bs); |
1039 | c6ca28d6 | aliguori | } |
1040 | c6ca28d6 | aliguori | |
1041 | f58c7b35 | ths | /*
|
1042 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
1043 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
1044 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
1045 | f58c7b35 | ths | *
|
1046 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
1047 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
1048 | f58c7b35 | ths | * allocated/unallocated state.
|
1049 | f58c7b35 | ths | *
|
1050 | f58c7b35 | ths | * 'nb_sectors' is the max value 'pnum' should be set to.
|
1051 | f58c7b35 | ths | */
|
1052 | f58c7b35 | ths | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
1053 | f58c7b35 | ths | int *pnum)
|
1054 | f58c7b35 | ths | { |
1055 | f58c7b35 | ths | int64_t n; |
1056 | f58c7b35 | ths | if (!bs->drv->bdrv_is_allocated) {
|
1057 | f58c7b35 | ths | if (sector_num >= bs->total_sectors) {
|
1058 | f58c7b35 | ths | *pnum = 0;
|
1059 | f58c7b35 | ths | return 0; |
1060 | f58c7b35 | ths | } |
1061 | f58c7b35 | ths | n = bs->total_sectors - sector_num; |
1062 | f58c7b35 | ths | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
1063 | f58c7b35 | ths | return 1; |
1064 | f58c7b35 | ths | } |
1065 | f58c7b35 | ths | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
|
1066 | f58c7b35 | ths | } |
1067 | f58c7b35 | ths | |
1068 | 376253ec | aliguori | void bdrv_info(Monitor *mon)
|
1069 | b338082b | bellard | { |
1070 | b338082b | bellard | BlockDriverState *bs; |
1071 | b338082b | bellard | |
1072 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1073 | 376253ec | aliguori | monitor_printf(mon, "%s:", bs->device_name);
|
1074 | 376253ec | aliguori | monitor_printf(mon, " type=");
|
1075 | b338082b | bellard | switch(bs->type) {
|
1076 | b338082b | bellard | case BDRV_TYPE_HD:
|
1077 | 376253ec | aliguori | monitor_printf(mon, "hd");
|
1078 | b338082b | bellard | break;
|
1079 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
1080 | 376253ec | aliguori | monitor_printf(mon, "cdrom");
|
1081 | b338082b | bellard | break;
|
1082 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
1083 | 376253ec | aliguori | monitor_printf(mon, "floppy");
|
1084 | b338082b | bellard | break;
|
1085 | b338082b | bellard | } |
1086 | 376253ec | aliguori | monitor_printf(mon, " removable=%d", bs->removable);
|
1087 | b338082b | bellard | if (bs->removable) {
|
1088 | 376253ec | aliguori | monitor_printf(mon, " locked=%d", bs->locked);
|
1089 | b338082b | bellard | } |
1090 | 19cb3738 | bellard | if (bs->drv) {
|
1091 | 376253ec | aliguori | monitor_printf(mon, " file=");
|
1092 | 376253ec | aliguori | monitor_print_filename(mon, bs->filename); |
1093 | fef30743 | ths | if (bs->backing_file[0] != '\0') { |
1094 | 376253ec | aliguori | monitor_printf(mon, " backing_file=");
|
1095 | 376253ec | aliguori | monitor_print_filename(mon, bs->backing_file); |
1096 | 376253ec | aliguori | } |
1097 | 376253ec | aliguori | monitor_printf(mon, " ro=%d", bs->read_only);
|
1098 | 376253ec | aliguori | monitor_printf(mon, " drv=%s", bs->drv->format_name);
|
1099 | 376253ec | aliguori | monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
|
1100 | b338082b | bellard | } else {
|
1101 | 376253ec | aliguori | monitor_printf(mon, " [not inserted]");
|
1102 | b338082b | bellard | } |
1103 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
1104 | b338082b | bellard | } |
1105 | b338082b | bellard | } |
1106 | a36e69dd | ths | |
1107 | a36e69dd | ths | /* The "info blockstats" command. */
|
1108 | 376253ec | aliguori | void bdrv_info_stats(Monitor *mon)
|
1109 | a36e69dd | ths | { |
1110 | a36e69dd | ths | BlockDriverState *bs; |
1111 | a36e69dd | ths | |
1112 | a36e69dd | ths | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1113 | 376253ec | aliguori | monitor_printf(mon, "%s:"
|
1114 | 376253ec | aliguori | " rd_bytes=%" PRIu64
|
1115 | 376253ec | aliguori | " wr_bytes=%" PRIu64
|
1116 | 376253ec | aliguori | " rd_operations=%" PRIu64
|
1117 | 376253ec | aliguori | " wr_operations=%" PRIu64
|
1118 | ebf53fcd | aliguori | "\n",
|
1119 | 376253ec | aliguori | bs->device_name, |
1120 | 376253ec | aliguori | bs->rd_bytes, bs->wr_bytes, |
1121 | 376253ec | aliguori | bs->rd_ops, bs->wr_ops); |
1122 | a36e69dd | ths | } |
1123 | a36e69dd | ths | } |
1124 | ea2384d3 | bellard | |
1125 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
1126 | 045df330 | aliguori | { |
1127 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
1128 | 045df330 | aliguori | return bs->backing_file;
|
1129 | 045df330 | aliguori | else if (bs->encrypted) |
1130 | 045df330 | aliguori | return bs->filename;
|
1131 | 045df330 | aliguori | else
|
1132 | 045df330 | aliguori | return NULL; |
1133 | 045df330 | aliguori | } |
1134 | 045df330 | aliguori | |
1135 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
1136 | 83f64091 | bellard | char *filename, int filename_size) |
1137 | 83f64091 | bellard | { |
1138 | 83f64091 | bellard | if (!bs->backing_hd) {
|
1139 | 83f64091 | bellard | pstrcpy(filename, filename_size, "");
|
1140 | 83f64091 | bellard | } else {
|
1141 | 83f64091 | bellard | pstrcpy(filename, filename_size, bs->backing_file); |
1142 | 83f64091 | bellard | } |
1143 | 83f64091 | bellard | } |
1144 | 83f64091 | bellard | |
1145 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
1146 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
1147 | faea38e7 | bellard | { |
1148 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1149 | faea38e7 | bellard | if (!drv)
|
1150 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1151 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
1152 | faea38e7 | bellard | return -ENOTSUP;
|
1153 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
1154 | faea38e7 | bellard | } |
1155 | 3b46e624 | ths | |
1156 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
1157 | faea38e7 | bellard | { |
1158 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1159 | faea38e7 | bellard | if (!drv)
|
1160 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1161 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
1162 | faea38e7 | bellard | return -ENOTSUP;
|
1163 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
1164 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
1165 | faea38e7 | bellard | } |
1166 | faea38e7 | bellard | |
1167 | 178e08a5 | aliguori | int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) |
1168 | 178e08a5 | aliguori | { |
1169 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
1170 | 178e08a5 | aliguori | if (!drv)
|
1171 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
1172 | 178e08a5 | aliguori | if (!drv->bdrv_put_buffer)
|
1173 | 178e08a5 | aliguori | return -ENOTSUP;
|
1174 | 178e08a5 | aliguori | return drv->bdrv_put_buffer(bs, buf, pos, size);
|
1175 | 178e08a5 | aliguori | } |
1176 | 178e08a5 | aliguori | |
1177 | 178e08a5 | aliguori | int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) |
1178 | 178e08a5 | aliguori | { |
1179 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
1180 | 178e08a5 | aliguori | if (!drv)
|
1181 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
1182 | 178e08a5 | aliguori | if (!drv->bdrv_get_buffer)
|
1183 | 178e08a5 | aliguori | return -ENOTSUP;
|
1184 | 178e08a5 | aliguori | return drv->bdrv_get_buffer(bs, buf, pos, size);
|
1185 | 178e08a5 | aliguori | } |
1186 | 178e08a5 | aliguori | |
1187 | faea38e7 | bellard | /**************************************************************/
|
1188 | faea38e7 | bellard | /* handling of snapshots */
|
1189 | faea38e7 | bellard | |
1190 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
1191 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
1192 | faea38e7 | bellard | { |
1193 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1194 | faea38e7 | bellard | if (!drv)
|
1195 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1196 | faea38e7 | bellard | if (!drv->bdrv_snapshot_create)
|
1197 | faea38e7 | bellard | return -ENOTSUP;
|
1198 | faea38e7 | bellard | return drv->bdrv_snapshot_create(bs, sn_info);
|
1199 | faea38e7 | bellard | } |
1200 | faea38e7 | bellard | |
1201 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
1202 | faea38e7 | bellard | const char *snapshot_id) |
1203 | faea38e7 | bellard | { |
1204 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1205 | faea38e7 | bellard | if (!drv)
|
1206 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1207 | faea38e7 | bellard | if (!drv->bdrv_snapshot_goto)
|
1208 | faea38e7 | bellard | return -ENOTSUP;
|
1209 | faea38e7 | bellard | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
1210 | faea38e7 | bellard | } |
1211 | faea38e7 | bellard | |
1212 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
1213 | faea38e7 | bellard | { |
1214 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1215 | faea38e7 | bellard | if (!drv)
|
1216 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1217 | faea38e7 | bellard | if (!drv->bdrv_snapshot_delete)
|
1218 | faea38e7 | bellard | return -ENOTSUP;
|
1219 | faea38e7 | bellard | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
1220 | faea38e7 | bellard | } |
1221 | faea38e7 | bellard | |
1222 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
1223 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
1224 | faea38e7 | bellard | { |
1225 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1226 | faea38e7 | bellard | if (!drv)
|
1227 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1228 | faea38e7 | bellard | if (!drv->bdrv_snapshot_list)
|
1229 | faea38e7 | bellard | return -ENOTSUP;
|
1230 | faea38e7 | bellard | return drv->bdrv_snapshot_list(bs, psn_info);
|
1231 | faea38e7 | bellard | } |
1232 | faea38e7 | bellard | |
1233 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
1234 | faea38e7 | bellard | |
1235 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
1236 | faea38e7 | bellard | { |
1237 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
1238 | faea38e7 | bellard | int64_t base; |
1239 | faea38e7 | bellard | int i;
|
1240 | faea38e7 | bellard | |
1241 | faea38e7 | bellard | if (size <= 999) { |
1242 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
1243 | faea38e7 | bellard | } else {
|
1244 | faea38e7 | bellard | base = 1024;
|
1245 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
1246 | faea38e7 | bellard | if (size < (10 * base)) { |
1247 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
1248 | faea38e7 | bellard | (double)size / base,
|
1249 | faea38e7 | bellard | suffixes[i]); |
1250 | faea38e7 | bellard | break;
|
1251 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
1252 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
1253 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
1254 | faea38e7 | bellard | suffixes[i]); |
1255 | faea38e7 | bellard | break;
|
1256 | faea38e7 | bellard | } |
1257 | faea38e7 | bellard | base = base * 1024;
|
1258 | faea38e7 | bellard | } |
1259 | faea38e7 | bellard | } |
1260 | faea38e7 | bellard | return buf;
|
1261 | faea38e7 | bellard | } |
1262 | faea38e7 | bellard | |
1263 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
1264 | faea38e7 | bellard | { |
1265 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
1266 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1267 | 3b9f94e1 | bellard | struct tm *ptm;
|
1268 | 3b9f94e1 | bellard | #else
|
1269 | faea38e7 | bellard | struct tm tm;
|
1270 | 3b9f94e1 | bellard | #endif
|
1271 | faea38e7 | bellard | time_t ti; |
1272 | faea38e7 | bellard | int64_t secs; |
1273 | faea38e7 | bellard | |
1274 | faea38e7 | bellard | if (!sn) {
|
1275 | 5fafdf24 | ths | snprintf(buf, buf_size, |
1276 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1277 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1278 | faea38e7 | bellard | } else {
|
1279 | faea38e7 | bellard | ti = sn->date_sec; |
1280 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1281 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
1282 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
1283 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
1284 | 3b9f94e1 | bellard | #else
|
1285 | faea38e7 | bellard | localtime_r(&ti, &tm); |
1286 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
1287 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
1288 | 3b9f94e1 | bellard | #endif
|
1289 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
1290 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
1291 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
1292 | faea38e7 | bellard | (int)(secs / 3600), |
1293 | faea38e7 | bellard | (int)((secs / 60) % 60), |
1294 | 5fafdf24 | ths | (int)(secs % 60), |
1295 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1296 | faea38e7 | bellard | snprintf(buf, buf_size, |
1297 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1298 | faea38e7 | bellard | sn->id_str, sn->name, |
1299 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
1300 | faea38e7 | bellard | date_buf, |
1301 | faea38e7 | bellard | clock_buf); |
1302 | faea38e7 | bellard | } |
1303 | faea38e7 | bellard | return buf;
|
1304 | faea38e7 | bellard | } |
1305 | faea38e7 | bellard | |
1306 | 83f64091 | bellard | |
1307 | ea2384d3 | bellard | /**************************************************************/
|
1308 | 83f64091 | bellard | /* async I/Os */
|
1309 | ea2384d3 | bellard | |
1310 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
1311 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
1312 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1313 | 3b69e4b9 | aliguori | { |
1314 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1315 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1316 | 83f64091 | bellard | |
1317 | 19cb3738 | bellard | if (!drv)
|
1318 | ce1a14dc | pbrook | return NULL; |
1319 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
1320 | 71d0770c | aliguori | return NULL; |
1321 | 3b46e624 | ths | |
1322 | f141eafe | aliguori | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
1323 | f141eafe | aliguori | cb, opaque); |
1324 | a36e69dd | ths | |
1325 | a36e69dd | ths | if (ret) {
|
1326 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1327 | a36e69dd | ths | bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
|
1328 | a36e69dd | ths | bs->rd_ops ++; |
1329 | a36e69dd | ths | } |
1330 | a36e69dd | ths | |
1331 | a36e69dd | ths | return ret;
|
1332 | ea2384d3 | bellard | } |
1333 | ea2384d3 | bellard | |
1334 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
1335 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
1336 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1337 | ea2384d3 | bellard | { |
1338 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1339 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1340 | ea2384d3 | bellard | |
1341 | 19cb3738 | bellard | if (!drv)
|
1342 | ce1a14dc | pbrook | return NULL; |
1343 | 83f64091 | bellard | if (bs->read_only)
|
1344 | ce1a14dc | pbrook | return NULL; |
1345 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
1346 | 71d0770c | aliguori | return NULL; |
1347 | 83f64091 | bellard | |
1348 | f141eafe | aliguori | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
1349 | f141eafe | aliguori | cb, opaque); |
1350 | a36e69dd | ths | |
1351 | a36e69dd | ths | if (ret) {
|
1352 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1353 | a36e69dd | ths | bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
|
1354 | a36e69dd | ths | bs->wr_ops ++; |
1355 | a36e69dd | ths | } |
1356 | a36e69dd | ths | |
1357 | a36e69dd | ths | return ret;
|
1358 | 83f64091 | bellard | } |
1359 | 83f64091 | bellard | |
1360 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
1361 | 83f64091 | bellard | { |
1362 | 6bbff9a0 | aliguori | acb->pool->cancel(acb); |
1363 | 83f64091 | bellard | } |
1364 | 83f64091 | bellard | |
1365 | ce1a14dc | pbrook | |
1366 | 83f64091 | bellard | /**************************************************************/
|
1367 | 83f64091 | bellard | /* async block device emulation */
|
1368 | 83f64091 | bellard | |
1369 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
1370 | 83f64091 | bellard | { |
1371 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
1372 | f141eafe | aliguori | |
1373 | f141eafe | aliguori | if (!acb->is_write)
|
1374 | f141eafe | aliguori | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
1375 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
1376 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
1377 | f141eafe | aliguori | |
1378 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1379 | 83f64091 | bellard | } |
1380 | beac80cd | bellard | |
1381 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
1382 | f141eafe | aliguori | int64_t sector_num, |
1383 | f141eafe | aliguori | QEMUIOVector *qiov, |
1384 | f141eafe | aliguori | int nb_sectors,
|
1385 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
1386 | f141eafe | aliguori | void *opaque,
|
1387 | f141eafe | aliguori | int is_write)
|
1388 | f141eafe | aliguori | |
1389 | 83f64091 | bellard | { |
1390 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
1391 | ce1a14dc | pbrook | |
1392 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
1393 | f141eafe | aliguori | acb->is_write = is_write; |
1394 | f141eafe | aliguori | acb->qiov = qiov; |
1395 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
1396 | f141eafe | aliguori | |
1397 | ce1a14dc | pbrook | if (!acb->bh)
|
1398 | ce1a14dc | pbrook | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
1399 | f141eafe | aliguori | |
1400 | f141eafe | aliguori | if (is_write) {
|
1401 | f141eafe | aliguori | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
1402 | f141eafe | aliguori | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
1403 | f141eafe | aliguori | } else {
|
1404 | f141eafe | aliguori | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
1405 | f141eafe | aliguori | } |
1406 | f141eafe | aliguori | |
1407 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
1408 | f141eafe | aliguori | |
1409 | ce1a14dc | pbrook | return &acb->common;
|
1410 | beac80cd | bellard | } |
1411 | beac80cd | bellard | |
1412 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
1413 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
1414 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1415 | beac80cd | bellard | { |
1416 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
1417 | f141eafe | aliguori | } |
1418 | 83f64091 | bellard | |
1419 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
1420 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
1421 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1422 | f141eafe | aliguori | { |
1423 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
1424 | beac80cd | bellard | } |
1425 | beac80cd | bellard | |
1426 | f141eafe | aliguori | |
1427 | ce1a14dc | pbrook | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
1428 | ea2384d3 | bellard | { |
1429 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
1430 | ce1a14dc | pbrook | qemu_bh_cancel(acb->bh); |
1431 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1432 | 83f64091 | bellard | } |
1433 | ea2384d3 | bellard | |
1434 | 83f64091 | bellard | /**************************************************************/
|
1435 | 83f64091 | bellard | /* sync block device emulation */
|
1436 | ea2384d3 | bellard | |
1437 | 83f64091 | bellard | static void bdrv_rw_em_cb(void *opaque, int ret) |
1438 | 83f64091 | bellard | { |
1439 | 83f64091 | bellard | *(int *)opaque = ret;
|
1440 | ea2384d3 | bellard | } |
1441 | ea2384d3 | bellard | |
1442 | 83f64091 | bellard | #define NOT_DONE 0x7fffffff |
1443 | 83f64091 | bellard | |
1444 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
1445 | 83f64091 | bellard | uint8_t *buf, int nb_sectors)
|
1446 | 7a6cba61 | pbrook | { |
1447 | ce1a14dc | pbrook | int async_ret;
|
1448 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1449 | f141eafe | aliguori | struct iovec iov;
|
1450 | f141eafe | aliguori | QEMUIOVector qiov; |
1451 | 83f64091 | bellard | |
1452 | 83f64091 | bellard | async_ret = NOT_DONE; |
1453 | 3f4cb3d3 | blueswir1 | iov.iov_base = (void *)buf;
|
1454 | f141eafe | aliguori | iov.iov_len = nb_sectors * 512;
|
1455 | f141eafe | aliguori | qemu_iovec_init_external(&qiov, &iov, 1);
|
1456 | f141eafe | aliguori | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
1457 | f141eafe | aliguori | bdrv_rw_em_cb, &async_ret); |
1458 | baf35cb9 | aliguori | if (acb == NULL) |
1459 | ce1a14dc | pbrook | return -1; |
1460 | baf35cb9 | aliguori | |
1461 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1462 | 83f64091 | bellard | qemu_aio_wait(); |
1463 | 83f64091 | bellard | } |
1464 | baf35cb9 | aliguori | |
1465 | 83f64091 | bellard | return async_ret;
|
1466 | 7a6cba61 | pbrook | } |
1467 | 7a6cba61 | pbrook | |
1468 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1469 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors) |
1470 | 83f64091 | bellard | { |
1471 | ce1a14dc | pbrook | int async_ret;
|
1472 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1473 | f141eafe | aliguori | struct iovec iov;
|
1474 | f141eafe | aliguori | QEMUIOVector qiov; |
1475 | 83f64091 | bellard | |
1476 | 83f64091 | bellard | async_ret = NOT_DONE; |
1477 | f141eafe | aliguori | iov.iov_base = (void *)buf;
|
1478 | f141eafe | aliguori | iov.iov_len = nb_sectors * 512;
|
1479 | f141eafe | aliguori | qemu_iovec_init_external(&qiov, &iov, 1);
|
1480 | f141eafe | aliguori | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
1481 | f141eafe | aliguori | bdrv_rw_em_cb, &async_ret); |
1482 | baf35cb9 | aliguori | if (acb == NULL) |
1483 | ce1a14dc | pbrook | return -1; |
1484 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1485 | 83f64091 | bellard | qemu_aio_wait(); |
1486 | 83f64091 | bellard | } |
1487 | 83f64091 | bellard | return async_ret;
|
1488 | 83f64091 | bellard | } |
1489 | ea2384d3 | bellard | |
1490 | ea2384d3 | bellard | void bdrv_init(void) |
1491 | ea2384d3 | bellard | { |
1492 | ea2384d3 | bellard | bdrv_register(&bdrv_raw); |
1493 | 19cb3738 | bellard | bdrv_register(&bdrv_host_device); |
1494 | ea2384d3 | bellard | #ifndef _WIN32
|
1495 | ea2384d3 | bellard | bdrv_register(&bdrv_cow); |
1496 | ea2384d3 | bellard | #endif
|
1497 | ea2384d3 | bellard | bdrv_register(&bdrv_qcow); |
1498 | ea2384d3 | bellard | bdrv_register(&bdrv_vmdk); |
1499 | 3c56521b | bellard | bdrv_register(&bdrv_cloop); |
1500 | 585d0ed9 | bellard | bdrv_register(&bdrv_dmg); |
1501 | a8753c34 | bellard | bdrv_register(&bdrv_bochs); |
1502 | 6a0f9e82 | bellard | bdrv_register(&bdrv_vpc); |
1503 | 712e7874 | bellard | bdrv_register(&bdrv_vvfat); |
1504 | faea38e7 | bellard | bdrv_register(&bdrv_qcow2); |
1505 | 6ada7453 | ths | bdrv_register(&bdrv_parallels); |
1506 | 75818250 | ths | bdrv_register(&bdrv_nbd); |
1507 | ea2384d3 | bellard | } |
1508 | ce1a14dc | pbrook | |
1509 | 6bbff9a0 | aliguori | void aio_pool_init(AIOPool *pool, int aiocb_size, |
1510 | 6bbff9a0 | aliguori | void (*cancel)(BlockDriverAIOCB *acb))
|
1511 | 6bbff9a0 | aliguori | { |
1512 | 6bbff9a0 | aliguori | pool->aiocb_size = aiocb_size; |
1513 | 6bbff9a0 | aliguori | pool->cancel = cancel; |
1514 | 6bbff9a0 | aliguori | pool->free_aiocb = NULL;
|
1515 | 6bbff9a0 | aliguori | } |
1516 | 6bbff9a0 | aliguori | |
1517 | 6bbff9a0 | aliguori | void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
|
1518 | 6bbff9a0 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1519 | ce1a14dc | pbrook | { |
1520 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1521 | ce1a14dc | pbrook | |
1522 | 6bbff9a0 | aliguori | if (pool->free_aiocb) {
|
1523 | 6bbff9a0 | aliguori | acb = pool->free_aiocb; |
1524 | 6bbff9a0 | aliguori | pool->free_aiocb = acb->next; |
1525 | ce1a14dc | pbrook | } else {
|
1526 | 6bbff9a0 | aliguori | acb = qemu_mallocz(pool->aiocb_size); |
1527 | 6bbff9a0 | aliguori | acb->pool = pool; |
1528 | ce1a14dc | pbrook | } |
1529 | ce1a14dc | pbrook | acb->bs = bs; |
1530 | ce1a14dc | pbrook | acb->cb = cb; |
1531 | ce1a14dc | pbrook | acb->opaque = opaque; |
1532 | ce1a14dc | pbrook | return acb;
|
1533 | ce1a14dc | pbrook | } |
1534 | ce1a14dc | pbrook | |
1535 | 6bbff9a0 | aliguori | void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
|
1536 | 6bbff9a0 | aliguori | void *opaque)
|
1537 | 6bbff9a0 | aliguori | { |
1538 | 6bbff9a0 | aliguori | return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
|
1539 | 6bbff9a0 | aliguori | } |
1540 | 6bbff9a0 | aliguori | |
1541 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
1542 | ce1a14dc | pbrook | { |
1543 | 6bbff9a0 | aliguori | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
1544 | 6bbff9a0 | aliguori | AIOPool *pool = acb->pool; |
1545 | 6bbff9a0 | aliguori | acb->next = pool->free_aiocb; |
1546 | 6bbff9a0 | aliguori | pool->free_aiocb = acb; |
1547 | ce1a14dc | pbrook | } |
1548 | 19cb3738 | bellard | |
1549 | 19cb3738 | bellard | /**************************************************************/
|
1550 | 19cb3738 | bellard | /* removable device support */
|
1551 | 19cb3738 | bellard | |
1552 | 19cb3738 | bellard | /**
|
1553 | 19cb3738 | bellard | * Return TRUE if the media is present
|
1554 | 19cb3738 | bellard | */
|
1555 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
1556 | 19cb3738 | bellard | { |
1557 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1558 | 19cb3738 | bellard | int ret;
|
1559 | 19cb3738 | bellard | if (!drv)
|
1560 | 19cb3738 | bellard | return 0; |
1561 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
1562 | 19cb3738 | bellard | return 1; |
1563 | 19cb3738 | bellard | ret = drv->bdrv_is_inserted(bs); |
1564 | 19cb3738 | bellard | return ret;
|
1565 | 19cb3738 | bellard | } |
1566 | 19cb3738 | bellard | |
1567 | 19cb3738 | bellard | /**
|
1568 | 19cb3738 | bellard | * Return TRUE if the media changed since the last call to this
|
1569 | 5fafdf24 | ths | * function. It is currently only used for floppy disks
|
1570 | 19cb3738 | bellard | */
|
1571 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
1572 | 19cb3738 | bellard | { |
1573 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1574 | 19cb3738 | bellard | int ret;
|
1575 | 19cb3738 | bellard | |
1576 | 19cb3738 | bellard | if (!drv || !drv->bdrv_media_changed)
|
1577 | 19cb3738 | bellard | ret = -ENOTSUP; |
1578 | 19cb3738 | bellard | else
|
1579 | 19cb3738 | bellard | ret = drv->bdrv_media_changed(bs); |
1580 | 19cb3738 | bellard | if (ret == -ENOTSUP)
|
1581 | 19cb3738 | bellard | ret = bs->media_changed; |
1582 | 19cb3738 | bellard | bs->media_changed = 0;
|
1583 | 19cb3738 | bellard | return ret;
|
1584 | 19cb3738 | bellard | } |
1585 | 19cb3738 | bellard | |
1586 | 19cb3738 | bellard | /**
|
1587 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
1588 | 19cb3738 | bellard | */
|
1589 | 19cb3738 | bellard | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
1590 | 19cb3738 | bellard | { |
1591 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1592 | 19cb3738 | bellard | int ret;
|
1593 | 19cb3738 | bellard | |
1594 | 19cb3738 | bellard | if (!drv || !drv->bdrv_eject) {
|
1595 | 19cb3738 | bellard | ret = -ENOTSUP; |
1596 | 19cb3738 | bellard | } else {
|
1597 | 19cb3738 | bellard | ret = drv->bdrv_eject(bs, eject_flag); |
1598 | 19cb3738 | bellard | } |
1599 | 19cb3738 | bellard | if (ret == -ENOTSUP) {
|
1600 | 19cb3738 | bellard | if (eject_flag)
|
1601 | 19cb3738 | bellard | bdrv_close(bs); |
1602 | 19cb3738 | bellard | } |
1603 | 19cb3738 | bellard | } |
1604 | 19cb3738 | bellard | |
1605 | 19cb3738 | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
1606 | 19cb3738 | bellard | { |
1607 | 19cb3738 | bellard | return bs->locked;
|
1608 | 19cb3738 | bellard | } |
1609 | 19cb3738 | bellard | |
1610 | 19cb3738 | bellard | /**
|
1611 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
1612 | 19cb3738 | bellard | * to eject it manually).
|
1613 | 19cb3738 | bellard | */
|
1614 | 19cb3738 | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
1615 | 19cb3738 | bellard | { |
1616 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1617 | 19cb3738 | bellard | |
1618 | 19cb3738 | bellard | bs->locked = locked; |
1619 | 19cb3738 | bellard | if (drv && drv->bdrv_set_locked) {
|
1620 | 19cb3738 | bellard | drv->bdrv_set_locked(bs, locked); |
1621 | 19cb3738 | bellard | } |
1622 | 19cb3738 | bellard | } |
1623 | 985a03b0 | ths | |
1624 | 985a03b0 | ths | /* needed for generic scsi interface */
|
1625 | 985a03b0 | ths | |
1626 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1627 | 985a03b0 | ths | { |
1628 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
1629 | 985a03b0 | ths | |
1630 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
1631 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
1632 | 985a03b0 | ths | return -ENOTSUP;
|
1633 | 985a03b0 | ths | } |
1634 | 7d780669 | aliguori | |
1635 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
1636 | 221f715d | aliguori | unsigned long int req, void *buf, |
1637 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1638 | 7d780669 | aliguori | { |
1639 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
1640 | 7d780669 | aliguori | |
1641 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
1642 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
1643 | 221f715d | aliguori | return NULL; |
1644 | 7d780669 | aliguori | } |
1645 | e268ca52 | aliguori | |
1646 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
1647 | e268ca52 | aliguori | { |
1648 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
1649 | e268ca52 | aliguori | } |