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