]>
Commit | Line | Data |
---|---|---|
62d23efa AL |
1 | /* |
2 | * xen paravirt block device backend | |
3 | * | |
4 | * (c) Gerd Hoffmann <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
8167ee88 | 16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
62d23efa AL |
17 | */ |
18 | ||
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdarg.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | #include <signal.h> | |
25 | #include <inttypes.h> | |
26 | #include <time.h> | |
27 | #include <fcntl.h> | |
28 | #include <errno.h> | |
29 | #include <sys/ioctl.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/mman.h> | |
33 | #include <sys/uio.h> | |
34 | ||
35 | #include <xs.h> | |
36 | #include <xenctrl.h> | |
37 | #include <xen/io/xenbus.h> | |
38 | ||
39 | #include "hw.h" | |
40 | #include "block_int.h" | |
41 | #include "qemu-char.h" | |
42 | #include "xen_blkif.h" | |
43 | #include "xen_backend.h" | |
2446333c | 44 | #include "blockdev.h" |
62d23efa AL |
45 | |
46 | /* ------------------------------------------------------------- */ | |
47 | ||
48 | static int syncwrite = 0; | |
49 | static int batch_maps = 0; | |
50 | ||
51 | static int max_requests = 32; | |
52 | static int use_aio = 1; | |
53 | ||
54 | /* ------------------------------------------------------------- */ | |
55 | ||
56 | #define BLOCK_SIZE 512 | |
57 | #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2) | |
58 | ||
59 | struct ioreq { | |
60 | blkif_request_t req; | |
61 | int16_t status; | |
62 | ||
63 | /* parsed request */ | |
64 | off_t start; | |
65 | QEMUIOVector v; | |
66 | int presync; | |
67 | int postsync; | |
68 | ||
69 | /* grant mapping */ | |
70 | uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
71 | uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
72 | int prot; | |
73 | void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
74 | void *pages; | |
75 | ||
76 | /* aio status */ | |
77 | int aio_inflight; | |
78 | int aio_errors; | |
79 | ||
80 | struct XenBlkDev *blkdev; | |
72cf2d4f | 81 | QLIST_ENTRY(ioreq) list; |
a597e79c | 82 | BlockAcctCookie acct; |
62d23efa AL |
83 | }; |
84 | ||
85 | struct XenBlkDev { | |
86 | struct XenDevice xendev; /* must be first */ | |
87 | char *params; | |
88 | char *mode; | |
89 | char *type; | |
90 | char *dev; | |
91 | char *devtype; | |
92 | const char *fileproto; | |
93 | const char *filename; | |
94 | int ring_ref; | |
95 | void *sring; | |
96 | int64_t file_blk; | |
97 | int64_t file_size; | |
98 | int protocol; | |
99 | blkif_back_rings_t rings; | |
100 | int more_work; | |
101 | int cnt_map; | |
102 | ||
103 | /* request lists */ | |
72cf2d4f BS |
104 | QLIST_HEAD(inflight_head, ioreq) inflight; |
105 | QLIST_HEAD(finished_head, ioreq) finished; | |
106 | QLIST_HEAD(freelist_head, ioreq) freelist; | |
62d23efa AL |
107 | int requests_total; |
108 | int requests_inflight; | |
109 | int requests_finished; | |
110 | ||
111 | /* qemu block driver */ | |
751c6a17 | 112 | DriveInfo *dinfo; |
62d23efa AL |
113 | BlockDriverState *bs; |
114 | QEMUBH *bh; | |
115 | }; | |
116 | ||
117 | /* ------------------------------------------------------------- */ | |
118 | ||
119 | static struct ioreq *ioreq_start(struct XenBlkDev *blkdev) | |
120 | { | |
121 | struct ioreq *ioreq = NULL; | |
122 | ||
72cf2d4f | 123 | if (QLIST_EMPTY(&blkdev->freelist)) { |
209cd7ab AP |
124 | if (blkdev->requests_total >= max_requests) { |
125 | goto out; | |
126 | } | |
127 | /* allocate new struct */ | |
7267c094 | 128 | ioreq = g_malloc0(sizeof(*ioreq)); |
209cd7ab AP |
129 | ioreq->blkdev = blkdev; |
130 | blkdev->requests_total++; | |
62d23efa AL |
131 | qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
132 | } else { | |
209cd7ab AP |
133 | /* get one from freelist */ |
134 | ioreq = QLIST_FIRST(&blkdev->freelist); | |
135 | QLIST_REMOVE(ioreq, list); | |
62d23efa AL |
136 | qemu_iovec_reset(&ioreq->v); |
137 | } | |
72cf2d4f | 138 | QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list); |
62d23efa AL |
139 | blkdev->requests_inflight++; |
140 | ||
141 | out: | |
142 | return ioreq; | |
143 | } | |
144 | ||
145 | static void ioreq_finish(struct ioreq *ioreq) | |
146 | { | |
147 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
148 | ||
72cf2d4f BS |
149 | QLIST_REMOVE(ioreq, list); |
150 | QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list); | |
62d23efa AL |
151 | blkdev->requests_inflight--; |
152 | blkdev->requests_finished++; | |
153 | } | |
154 | ||
155 | static void ioreq_release(struct ioreq *ioreq) | |
156 | { | |
157 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
158 | ||
72cf2d4f | 159 | QLIST_REMOVE(ioreq, list); |
62d23efa AL |
160 | memset(ioreq, 0, sizeof(*ioreq)); |
161 | ioreq->blkdev = blkdev; | |
72cf2d4f | 162 | QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list); |
62d23efa AL |
163 | blkdev->requests_finished--; |
164 | } | |
165 | ||
166 | /* | |
167 | * translate request into iovec + start offset | |
168 | * do sanity checks along the way | |
169 | */ | |
170 | static int ioreq_parse(struct ioreq *ioreq) | |
171 | { | |
172 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
173 | uintptr_t mem; | |
174 | size_t len; | |
175 | int i; | |
176 | ||
177 | xen_be_printf(&blkdev->xendev, 3, | |
209cd7ab AP |
178 | "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n", |
179 | ioreq->req.operation, ioreq->req.nr_segments, | |
180 | ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number); | |
62d23efa AL |
181 | switch (ioreq->req.operation) { |
182 | case BLKIF_OP_READ: | |
209cd7ab AP |
183 | ioreq->prot = PROT_WRITE; /* to memory */ |
184 | break; | |
62d23efa | 185 | case BLKIF_OP_WRITE_BARRIER: |
5cbdebe3 SS |
186 | if (!ioreq->req.nr_segments) { |
187 | ioreq->presync = 1; | |
188 | return 0; | |
189 | } | |
209cd7ab AP |
190 | if (!syncwrite) { |
191 | ioreq->presync = ioreq->postsync = 1; | |
192 | } | |
193 | /* fall through */ | |
62d23efa | 194 | case BLKIF_OP_WRITE: |
209cd7ab AP |
195 | ioreq->prot = PROT_READ; /* from memory */ |
196 | if (syncwrite) { | |
197 | ioreq->postsync = 1; | |
198 | } | |
199 | break; | |
62d23efa | 200 | default: |
209cd7ab AP |
201 | xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n", |
202 | ioreq->req.operation); | |
203 | goto err; | |
62d23efa AL |
204 | }; |
205 | ||
908c7b9f GH |
206 | if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') { |
207 | xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n"); | |
208 | goto err; | |
209 | } | |
210 | ||
62d23efa AL |
211 | ioreq->start = ioreq->req.sector_number * blkdev->file_blk; |
212 | for (i = 0; i < ioreq->req.nr_segments; i++) { | |
209cd7ab AP |
213 | if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) { |
214 | xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n"); | |
215 | goto err; | |
216 | } | |
217 | if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) { | |
218 | xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n"); | |
219 | goto err; | |
220 | } | |
221 | if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) { | |
222 | xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n"); | |
223 | goto err; | |
224 | } | |
225 | ||
226 | ioreq->domids[i] = blkdev->xendev.dom; | |
227 | ioreq->refs[i] = ioreq->req.seg[i].gref; | |
228 | ||
229 | mem = ioreq->req.seg[i].first_sect * blkdev->file_blk; | |
230 | len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk; | |
62d23efa AL |
231 | qemu_iovec_add(&ioreq->v, (void*)mem, len); |
232 | } | |
233 | if (ioreq->start + ioreq->v.size > blkdev->file_size) { | |
209cd7ab AP |
234 | xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n"); |
235 | goto err; | |
62d23efa AL |
236 | } |
237 | return 0; | |
238 | ||
239 | err: | |
240 | ioreq->status = BLKIF_RSP_ERROR; | |
241 | return -1; | |
242 | } | |
243 | ||
244 | static void ioreq_unmap(struct ioreq *ioreq) | |
245 | { | |
d5b93ddf | 246 | XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev; |
62d23efa AL |
247 | int i; |
248 | ||
209cd7ab | 249 | if (ioreq->v.niov == 0) { |
62d23efa | 250 | return; |
209cd7ab | 251 | } |
62d23efa | 252 | if (batch_maps) { |
209cd7ab AP |
253 | if (!ioreq->pages) { |
254 | return; | |
255 | } | |
256 | if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->v.niov) != 0) { | |
257 | xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n", | |
258 | strerror(errno)); | |
259 | } | |
260 | ioreq->blkdev->cnt_map -= ioreq->v.niov; | |
261 | ioreq->pages = NULL; | |
62d23efa | 262 | } else { |
209cd7ab AP |
263 | for (i = 0; i < ioreq->v.niov; i++) { |
264 | if (!ioreq->page[i]) { | |
265 | continue; | |
266 | } | |
267 | if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) { | |
268 | xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n", | |
269 | strerror(errno)); | |
270 | } | |
271 | ioreq->blkdev->cnt_map--; | |
272 | ioreq->page[i] = NULL; | |
273 | } | |
62d23efa AL |
274 | } |
275 | } | |
276 | ||
277 | static int ioreq_map(struct ioreq *ioreq) | |
278 | { | |
d5b93ddf | 279 | XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev; |
62d23efa AL |
280 | int i; |
281 | ||
209cd7ab | 282 | if (ioreq->v.niov == 0) { |
62d23efa | 283 | return 0; |
209cd7ab | 284 | } |
62d23efa | 285 | if (batch_maps) { |
209cd7ab AP |
286 | ioreq->pages = xc_gnttab_map_grant_refs |
287 | (gnt, ioreq->v.niov, ioreq->domids, ioreq->refs, ioreq->prot); | |
288 | if (ioreq->pages == NULL) { | |
289 | xen_be_printf(&ioreq->blkdev->xendev, 0, | |
290 | "can't map %d grant refs (%s, %d maps)\n", | |
291 | ioreq->v.niov, strerror(errno), ioreq->blkdev->cnt_map); | |
292 | return -1; | |
293 | } | |
294 | for (i = 0; i < ioreq->v.niov; i++) { | |
295 | ioreq->v.iov[i].iov_base = ioreq->pages + i * XC_PAGE_SIZE + | |
296 | (uintptr_t)ioreq->v.iov[i].iov_base; | |
297 | } | |
298 | ioreq->blkdev->cnt_map += ioreq->v.niov; | |
62d23efa | 299 | } else { |
209cd7ab AP |
300 | for (i = 0; i < ioreq->v.niov; i++) { |
301 | ioreq->page[i] = xc_gnttab_map_grant_ref | |
302 | (gnt, ioreq->domids[i], ioreq->refs[i], ioreq->prot); | |
303 | if (ioreq->page[i] == NULL) { | |
304 | xen_be_printf(&ioreq->blkdev->xendev, 0, | |
305 | "can't map grant ref %d (%s, %d maps)\n", | |
306 | ioreq->refs[i], strerror(errno), ioreq->blkdev->cnt_map); | |
307 | ioreq_unmap(ioreq); | |
308 | return -1; | |
309 | } | |
310 | ioreq->v.iov[i].iov_base = ioreq->page[i] + (uintptr_t)ioreq->v.iov[i].iov_base; | |
311 | ioreq->blkdev->cnt_map++; | |
312 | } | |
62d23efa AL |
313 | } |
314 | return 0; | |
315 | } | |
316 | ||
317 | static int ioreq_runio_qemu_sync(struct ioreq *ioreq) | |
318 | { | |
319 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
1e71db30 | 320 | int i, rc; |
62d23efa AL |
321 | off_t pos; |
322 | ||
209cd7ab AP |
323 | if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) { |
324 | goto err_no_map; | |
325 | } | |
326 | if (ioreq->presync) { | |
327 | bdrv_flush(blkdev->bs); | |
328 | } | |
62d23efa AL |
329 | |
330 | switch (ioreq->req.operation) { | |
331 | case BLKIF_OP_READ: | |
209cd7ab AP |
332 | pos = ioreq->start; |
333 | for (i = 0; i < ioreq->v.niov; i++) { | |
334 | rc = bdrv_read(blkdev->bs, pos / BLOCK_SIZE, | |
335 | ioreq->v.iov[i].iov_base, | |
336 | ioreq->v.iov[i].iov_len / BLOCK_SIZE); | |
337 | if (rc != 0) { | |
338 | xen_be_printf(&blkdev->xendev, 0, "rd I/O error (%p, len %zd)\n", | |
339 | ioreq->v.iov[i].iov_base, | |
340 | ioreq->v.iov[i].iov_len); | |
341 | goto err; | |
342 | } | |
209cd7ab AP |
343 | pos += ioreq->v.iov[i].iov_len; |
344 | } | |
345 | break; | |
62d23efa AL |
346 | case BLKIF_OP_WRITE: |
347 | case BLKIF_OP_WRITE_BARRIER: | |
209cd7ab | 348 | if (!ioreq->req.nr_segments) { |
5cbdebe3 | 349 | break; |
209cd7ab AP |
350 | } |
351 | pos = ioreq->start; | |
352 | for (i = 0; i < ioreq->v.niov; i++) { | |
353 | rc = bdrv_write(blkdev->bs, pos / BLOCK_SIZE, | |
354 | ioreq->v.iov[i].iov_base, | |
355 | ioreq->v.iov[i].iov_len / BLOCK_SIZE); | |
356 | if (rc != 0) { | |
357 | xen_be_printf(&blkdev->xendev, 0, "wr I/O error (%p, len %zd)\n", | |
358 | ioreq->v.iov[i].iov_base, | |
359 | ioreq->v.iov[i].iov_len); | |
360 | goto err; | |
361 | } | |
209cd7ab AP |
362 | pos += ioreq->v.iov[i].iov_len; |
363 | } | |
364 | break; | |
62d23efa | 365 | default: |
209cd7ab AP |
366 | /* unknown operation (shouldn't happen -- parse catches this) */ |
367 | goto err; | |
62d23efa AL |
368 | } |
369 | ||
209cd7ab AP |
370 | if (ioreq->postsync) { |
371 | bdrv_flush(blkdev->bs); | |
372 | } | |
62d23efa AL |
373 | ioreq->status = BLKIF_RSP_OKAY; |
374 | ||
375 | ioreq_unmap(ioreq); | |
376 | ioreq_finish(ioreq); | |
377 | return 0; | |
378 | ||
379 | err: | |
f6ec953c FZ |
380 | ioreq_unmap(ioreq); |
381 | err_no_map: | |
382 | ioreq_finish(ioreq); | |
62d23efa AL |
383 | ioreq->status = BLKIF_RSP_ERROR; |
384 | return -1; | |
385 | } | |
386 | ||
387 | static void qemu_aio_complete(void *opaque, int ret) | |
388 | { | |
389 | struct ioreq *ioreq = opaque; | |
390 | ||
391 | if (ret != 0) { | |
392 | xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n", | |
393 | ioreq->req.operation == BLKIF_OP_READ ? "read" : "write"); | |
394 | ioreq->aio_errors++; | |
395 | } | |
396 | ||
397 | ioreq->aio_inflight--; | |
209cd7ab | 398 | if (ioreq->aio_inflight > 0) { |
62d23efa | 399 | return; |
209cd7ab | 400 | } |
62d23efa AL |
401 | |
402 | ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY; | |
403 | ioreq_unmap(ioreq); | |
404 | ioreq_finish(ioreq); | |
a597e79c | 405 | bdrv_acct_done(ioreq->blkdev->bs, &ioreq->acct); |
62d23efa AL |
406 | qemu_bh_schedule(ioreq->blkdev->bh); |
407 | } | |
408 | ||
409 | static int ioreq_runio_qemu_aio(struct ioreq *ioreq) | |
410 | { | |
411 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
412 | ||
209cd7ab AP |
413 | if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) { |
414 | goto err_no_map; | |
415 | } | |
62d23efa AL |
416 | |
417 | ioreq->aio_inflight++; | |
209cd7ab AP |
418 | if (ioreq->presync) { |
419 | bdrv_flush(blkdev->bs); /* FIXME: aio_flush() ??? */ | |
420 | } | |
62d23efa AL |
421 | |
422 | switch (ioreq->req.operation) { | |
423 | case BLKIF_OP_READ: | |
a597e79c | 424 | bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_READ); |
62d23efa AL |
425 | ioreq->aio_inflight++; |
426 | bdrv_aio_readv(blkdev->bs, ioreq->start / BLOCK_SIZE, | |
427 | &ioreq->v, ioreq->v.size / BLOCK_SIZE, | |
428 | qemu_aio_complete, ioreq); | |
209cd7ab | 429 | break; |
62d23efa AL |
430 | case BLKIF_OP_WRITE: |
431 | case BLKIF_OP_WRITE_BARRIER: | |
209cd7ab | 432 | if (!ioreq->req.nr_segments) { |
5cbdebe3 | 433 | break; |
209cd7ab | 434 | } |
a597e79c CH |
435 | |
436 | bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_WRITE); | |
209bef3e | 437 | ioreq->aio_inflight++; |
62d23efa AL |
438 | bdrv_aio_writev(blkdev->bs, ioreq->start / BLOCK_SIZE, |
439 | &ioreq->v, ioreq->v.size / BLOCK_SIZE, | |
440 | qemu_aio_complete, ioreq); | |
209cd7ab | 441 | break; |
62d23efa | 442 | default: |
209cd7ab AP |
443 | /* unknown operation (shouldn't happen -- parse catches this) */ |
444 | goto err; | |
62d23efa AL |
445 | } |
446 | ||
209cd7ab AP |
447 | if (ioreq->postsync) { |
448 | bdrv_flush(blkdev->bs); /* FIXME: aio_flush() ??? */ | |
449 | } | |
62d23efa AL |
450 | qemu_aio_complete(ioreq, 0); |
451 | ||
452 | return 0; | |
453 | ||
454 | err: | |
f6ec953c FZ |
455 | ioreq_unmap(ioreq); |
456 | err_no_map: | |
457 | ioreq_finish(ioreq); | |
62d23efa AL |
458 | ioreq->status = BLKIF_RSP_ERROR; |
459 | return -1; | |
460 | } | |
461 | ||
462 | static int blk_send_response_one(struct ioreq *ioreq) | |
463 | { | |
464 | struct XenBlkDev *blkdev = ioreq->blkdev; | |
465 | int send_notify = 0; | |
466 | int have_requests = 0; | |
467 | blkif_response_t resp; | |
468 | void *dst; | |
469 | ||
470 | resp.id = ioreq->req.id; | |
471 | resp.operation = ioreq->req.operation; | |
472 | resp.status = ioreq->status; | |
473 | ||
474 | /* Place on the response ring for the relevant domain. */ | |
475 | switch (blkdev->protocol) { | |
476 | case BLKIF_PROTOCOL_NATIVE: | |
209cd7ab AP |
477 | dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt); |
478 | break; | |
62d23efa | 479 | case BLKIF_PROTOCOL_X86_32: |
6fcfeff9 BS |
480 | dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part, |
481 | blkdev->rings.x86_32_part.rsp_prod_pvt); | |
209cd7ab | 482 | break; |
62d23efa | 483 | case BLKIF_PROTOCOL_X86_64: |
6fcfeff9 BS |
484 | dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part, |
485 | blkdev->rings.x86_64_part.rsp_prod_pvt); | |
209cd7ab | 486 | break; |
62d23efa | 487 | default: |
209cd7ab | 488 | dst = NULL; |
62d23efa AL |
489 | } |
490 | memcpy(dst, &resp, sizeof(resp)); | |
491 | blkdev->rings.common.rsp_prod_pvt++; | |
492 | ||
493 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify); | |
494 | if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) { | |
209cd7ab AP |
495 | /* |
496 | * Tail check for pending requests. Allows frontend to avoid | |
497 | * notifications if requests are already in flight (lower | |
498 | * overheads and promotes batching). | |
499 | */ | |
500 | RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests); | |
62d23efa | 501 | } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) { |
209cd7ab | 502 | have_requests = 1; |
62d23efa AL |
503 | } |
504 | ||
209cd7ab AP |
505 | if (have_requests) { |
506 | blkdev->more_work++; | |
507 | } | |
62d23efa AL |
508 | return send_notify; |
509 | } | |
510 | ||
511 | /* walk finished list, send outstanding responses, free requests */ | |
512 | static void blk_send_response_all(struct XenBlkDev *blkdev) | |
513 | { | |
514 | struct ioreq *ioreq; | |
515 | int send_notify = 0; | |
516 | ||
72cf2d4f BS |
517 | while (!QLIST_EMPTY(&blkdev->finished)) { |
518 | ioreq = QLIST_FIRST(&blkdev->finished); | |
209cd7ab AP |
519 | send_notify += blk_send_response_one(ioreq); |
520 | ioreq_release(ioreq); | |
521 | } | |
522 | if (send_notify) { | |
523 | xen_be_send_notify(&blkdev->xendev); | |
62d23efa | 524 | } |
62d23efa AL |
525 | } |
526 | ||
527 | static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc) | |
528 | { | |
529 | switch (blkdev->protocol) { | |
530 | case BLKIF_PROTOCOL_NATIVE: | |
209cd7ab AP |
531 | memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc), |
532 | sizeof(ioreq->req)); | |
533 | break; | |
62d23efa | 534 | case BLKIF_PROTOCOL_X86_32: |
6fcfeff9 BS |
535 | blkif_get_x86_32_req(&ioreq->req, |
536 | RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc)); | |
209cd7ab | 537 | break; |
62d23efa | 538 | case BLKIF_PROTOCOL_X86_64: |
6fcfeff9 BS |
539 | blkif_get_x86_64_req(&ioreq->req, |
540 | RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc)); | |
209cd7ab | 541 | break; |
62d23efa AL |
542 | } |
543 | return 0; | |
544 | } | |
545 | ||
546 | static void blk_handle_requests(struct XenBlkDev *blkdev) | |
547 | { | |
548 | RING_IDX rc, rp; | |
549 | struct ioreq *ioreq; | |
550 | ||
551 | blkdev->more_work = 0; | |
552 | ||
553 | rc = blkdev->rings.common.req_cons; | |
554 | rp = blkdev->rings.common.sring->req_prod; | |
555 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
556 | ||
209cd7ab | 557 | if (use_aio) { |
62d23efa | 558 | blk_send_response_all(blkdev); |
209cd7ab | 559 | } |
fc1f79f7 | 560 | while (rc != rp) { |
62d23efa | 561 | /* pull request from ring */ |
209cd7ab | 562 | if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) { |
62d23efa | 563 | break; |
209cd7ab | 564 | } |
62d23efa AL |
565 | ioreq = ioreq_start(blkdev); |
566 | if (ioreq == NULL) { | |
567 | blkdev->more_work++; | |
568 | break; | |
569 | } | |
570 | blk_get_request(blkdev, ioreq, rc); | |
571 | blkdev->rings.common.req_cons = ++rc; | |
572 | ||
573 | /* parse them */ | |
574 | if (ioreq_parse(ioreq) != 0) { | |
209cd7ab | 575 | if (blk_send_response_one(ioreq)) { |
62d23efa | 576 | xen_be_send_notify(&blkdev->xendev); |
209cd7ab | 577 | } |
62d23efa AL |
578 | ioreq_release(ioreq); |
579 | continue; | |
580 | } | |
581 | ||
582 | if (use_aio) { | |
583 | /* run i/o in aio mode */ | |
584 | ioreq_runio_qemu_aio(ioreq); | |
585 | } else { | |
586 | /* run i/o in sync mode */ | |
587 | ioreq_runio_qemu_sync(ioreq); | |
588 | } | |
589 | } | |
209cd7ab | 590 | if (!use_aio) { |
62d23efa | 591 | blk_send_response_all(blkdev); |
209cd7ab | 592 | } |
62d23efa | 593 | |
209cd7ab | 594 | if (blkdev->more_work && blkdev->requests_inflight < max_requests) { |
62d23efa | 595 | qemu_bh_schedule(blkdev->bh); |
209cd7ab | 596 | } |
62d23efa AL |
597 | } |
598 | ||
599 | /* ------------------------------------------------------------- */ | |
600 | ||
601 | static void blk_bh(void *opaque) | |
602 | { | |
603 | struct XenBlkDev *blkdev = opaque; | |
604 | blk_handle_requests(blkdev); | |
605 | } | |
606 | ||
607 | static void blk_alloc(struct XenDevice *xendev) | |
608 | { | |
609 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
610 | ||
72cf2d4f BS |
611 | QLIST_INIT(&blkdev->inflight); |
612 | QLIST_INIT(&blkdev->finished); | |
613 | QLIST_INIT(&blkdev->freelist); | |
62d23efa | 614 | blkdev->bh = qemu_bh_new(blk_bh, blkdev); |
209cd7ab | 615 | if (xen_mode != XEN_EMULATE) { |
62d23efa | 616 | batch_maps = 1; |
209cd7ab | 617 | } |
62d23efa AL |
618 | } |
619 | ||
620 | static int blk_init(struct XenDevice *xendev) | |
621 | { | |
622 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
5cce43bb | 623 | int index, qflags, info = 0; |
62d23efa AL |
624 | |
625 | /* read xenstore entries */ | |
626 | if (blkdev->params == NULL) { | |
5ea3c2b4 | 627 | char *h = NULL; |
209cd7ab | 628 | blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params"); |
5ea3c2b4 SS |
629 | if (blkdev->params != NULL) { |
630 | h = strchr(blkdev->params, ':'); | |
631 | } | |
209cd7ab AP |
632 | if (h != NULL) { |
633 | blkdev->fileproto = blkdev->params; | |
634 | blkdev->filename = h+1; | |
635 | *h = 0; | |
636 | } else { | |
637 | blkdev->fileproto = "<unset>"; | |
638 | blkdev->filename = blkdev->params; | |
639 | } | |
640 | } | |
7cef3f4f SS |
641 | if (!strcmp("aio", blkdev->fileproto)) { |
642 | blkdev->fileproto = "raw"; | |
643 | } | |
209cd7ab AP |
644 | if (blkdev->mode == NULL) { |
645 | blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode"); | |
646 | } | |
647 | if (blkdev->type == NULL) { | |
648 | blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type"); | |
649 | } | |
650 | if (blkdev->dev == NULL) { | |
651 | blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev"); | |
652 | } | |
653 | if (blkdev->devtype == NULL) { | |
654 | blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type"); | |
655 | } | |
62d23efa AL |
656 | |
657 | /* do we have all we need? */ | |
658 | if (blkdev->params == NULL || | |
209cd7ab AP |
659 | blkdev->mode == NULL || |
660 | blkdev->type == NULL || | |
661 | blkdev->dev == NULL) { | |
5ea3c2b4 | 662 | goto out_error; |
209cd7ab | 663 | } |
62d23efa AL |
664 | |
665 | /* read-only ? */ | |
666 | if (strcmp(blkdev->mode, "w") == 0) { | |
209cd7ab | 667 | qflags = BDRV_O_RDWR; |
62d23efa | 668 | } else { |
209cd7ab AP |
669 | qflags = 0; |
670 | info |= VDISK_READONLY; | |
62d23efa AL |
671 | } |
672 | ||
673 | /* cdrom ? */ | |
209cd7ab AP |
674 | if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) { |
675 | info |= VDISK_CDROM; | |
676 | } | |
62d23efa AL |
677 | |
678 | /* init qemu block driver */ | |
751c6a17 GH |
679 | index = (blkdev->xendev.dev - 202 * 256) / 16; |
680 | blkdev->dinfo = drive_get(IF_XEN, 0, index); | |
681 | if (!blkdev->dinfo) { | |
62d23efa AL |
682 | /* setup via xenbus -> create new block driver instance */ |
683 | xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n"); | |
ad717139 | 684 | blkdev->bs = bdrv_new(blkdev->dev); |
5ea3c2b4 SS |
685 | if (blkdev->bs) { |
686 | if (bdrv_open(blkdev->bs, blkdev->filename, qflags, | |
687 | bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) { | |
688 | bdrv_delete(blkdev->bs); | |
689 | blkdev->bs = NULL; | |
690 | } | |
691 | } | |
692 | if (!blkdev->bs) { | |
693 | goto out_error; | |
ad717139 | 694 | } |
62d23efa AL |
695 | } else { |
696 | /* setup via qemu cmdline -> already setup for us */ | |
697 | xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n"); | |
209cd7ab | 698 | blkdev->bs = blkdev->dinfo->bdrv; |
62d23efa | 699 | } |
fa879d62 | 700 | bdrv_attach_dev_nofail(blkdev->bs, blkdev); |
62d23efa AL |
701 | blkdev->file_blk = BLOCK_SIZE; |
702 | blkdev->file_size = bdrv_getlength(blkdev->bs); | |
703 | if (blkdev->file_size < 0) { | |
704 | xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n", | |
705 | (int)blkdev->file_size, strerror(-blkdev->file_size), | |
706 | blkdev->bs->drv ? blkdev->bs->drv->format_name : "-"); | |
209cd7ab | 707 | blkdev->file_size = 0; |
62d23efa | 708 | } |
62d23efa AL |
709 | |
710 | xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\"," | |
209cd7ab AP |
711 | " size %" PRId64 " (%" PRId64 " MB)\n", |
712 | blkdev->type, blkdev->fileproto, blkdev->filename, | |
713 | blkdev->file_size, blkdev->file_size >> 20); | |
62d23efa AL |
714 | |
715 | /* fill info */ | |
5cce43bb | 716 | xenstore_write_be_int(&blkdev->xendev, "feature-barrier", 1); |
62d23efa AL |
717 | xenstore_write_be_int(&blkdev->xendev, "info", info); |
718 | xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk); | |
719 | xenstore_write_be_int(&blkdev->xendev, "sectors", | |
209cd7ab | 720 | blkdev->file_size / blkdev->file_blk); |
62d23efa | 721 | return 0; |
5ea3c2b4 SS |
722 | |
723 | out_error: | |
7267c094 | 724 | g_free(blkdev->params); |
5ea3c2b4 | 725 | blkdev->params = NULL; |
7267c094 | 726 | g_free(blkdev->mode); |
5ea3c2b4 | 727 | blkdev->mode = NULL; |
7267c094 | 728 | g_free(blkdev->type); |
5ea3c2b4 | 729 | blkdev->type = NULL; |
7267c094 | 730 | g_free(blkdev->dev); |
5ea3c2b4 | 731 | blkdev->dev = NULL; |
7267c094 | 732 | g_free(blkdev->devtype); |
5ea3c2b4 SS |
733 | blkdev->devtype = NULL; |
734 | return -1; | |
62d23efa AL |
735 | } |
736 | ||
737 | static int blk_connect(struct XenDevice *xendev) | |
738 | { | |
739 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
740 | ||
209cd7ab AP |
741 | if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) { |
742 | return -1; | |
743 | } | |
62d23efa | 744 | if (xenstore_read_fe_int(&blkdev->xendev, "event-channel", |
209cd7ab AP |
745 | &blkdev->xendev.remote_port) == -1) { |
746 | return -1; | |
747 | } | |
62d23efa AL |
748 | |
749 | blkdev->protocol = BLKIF_PROTOCOL_NATIVE; | |
750 | if (blkdev->xendev.protocol) { | |
209cd7ab | 751 | if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) { |
62d23efa | 752 | blkdev->protocol = BLKIF_PROTOCOL_X86_32; |
209cd7ab AP |
753 | } |
754 | if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) { | |
62d23efa | 755 | blkdev->protocol = BLKIF_PROTOCOL_X86_64; |
209cd7ab | 756 | } |
62d23efa AL |
757 | } |
758 | ||
759 | blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev, | |
209cd7ab AP |
760 | blkdev->xendev.dom, |
761 | blkdev->ring_ref, | |
762 | PROT_READ | PROT_WRITE); | |
763 | if (!blkdev->sring) { | |
764 | return -1; | |
765 | } | |
62d23efa AL |
766 | blkdev->cnt_map++; |
767 | ||
768 | switch (blkdev->protocol) { | |
769 | case BLKIF_PROTOCOL_NATIVE: | |
770 | { | |
209cd7ab AP |
771 | blkif_sring_t *sring_native = blkdev->sring; |
772 | BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE); | |
773 | break; | |
62d23efa AL |
774 | } |
775 | case BLKIF_PROTOCOL_X86_32: | |
776 | { | |
209cd7ab | 777 | blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring; |
6fcfeff9 BS |
778 | |
779 | BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE); | |
209cd7ab | 780 | break; |
62d23efa AL |
781 | } |
782 | case BLKIF_PROTOCOL_X86_64: | |
783 | { | |
209cd7ab | 784 | blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring; |
6fcfeff9 BS |
785 | |
786 | BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE); | |
209cd7ab | 787 | break; |
62d23efa AL |
788 | } |
789 | } | |
790 | ||
791 | xen_be_bind_evtchn(&blkdev->xendev); | |
792 | ||
793 | xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, " | |
209cd7ab AP |
794 | "remote port %d, local port %d\n", |
795 | blkdev->xendev.protocol, blkdev->ring_ref, | |
796 | blkdev->xendev.remote_port, blkdev->xendev.local_port); | |
62d23efa AL |
797 | return 0; |
798 | } | |
799 | ||
800 | static void blk_disconnect(struct XenDevice *xendev) | |
801 | { | |
802 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
803 | ||
804 | if (blkdev->bs) { | |
751c6a17 | 805 | if (!blkdev->dinfo) { |
62d23efa AL |
806 | /* close/delete only if we created it ourself */ |
807 | bdrv_close(blkdev->bs); | |
808 | bdrv_delete(blkdev->bs); | |
809 | } | |
209cd7ab | 810 | blkdev->bs = NULL; |
62d23efa AL |
811 | } |
812 | xen_be_unbind_evtchn(&blkdev->xendev); | |
813 | ||
814 | if (blkdev->sring) { | |
209cd7ab AP |
815 | xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1); |
816 | blkdev->cnt_map--; | |
817 | blkdev->sring = NULL; | |
62d23efa AL |
818 | } |
819 | } | |
820 | ||
821 | static int blk_free(struct XenDevice *xendev) | |
822 | { | |
823 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
824 | struct ioreq *ioreq; | |
825 | ||
72cf2d4f | 826 | while (!QLIST_EMPTY(&blkdev->freelist)) { |
209cd7ab | 827 | ioreq = QLIST_FIRST(&blkdev->freelist); |
72cf2d4f | 828 | QLIST_REMOVE(ioreq, list); |
62d23efa | 829 | qemu_iovec_destroy(&ioreq->v); |
7267c094 | 830 | g_free(ioreq); |
62d23efa AL |
831 | } |
832 | ||
7267c094 AL |
833 | g_free(blkdev->params); |
834 | g_free(blkdev->mode); | |
835 | g_free(blkdev->type); | |
836 | g_free(blkdev->dev); | |
837 | g_free(blkdev->devtype); | |
62d23efa AL |
838 | qemu_bh_delete(blkdev->bh); |
839 | return 0; | |
840 | } | |
841 | ||
842 | static void blk_event(struct XenDevice *xendev) | |
843 | { | |
844 | struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev); | |
845 | ||
846 | qemu_bh_schedule(blkdev->bh); | |
847 | } | |
848 | ||
849 | struct XenDevOps xen_blkdev_ops = { | |
850 | .size = sizeof(struct XenBlkDev), | |
851 | .flags = DEVOPS_FLAG_NEED_GNTDEV, | |
852 | .alloc = blk_alloc, | |
853 | .init = blk_init, | |
384087b2 | 854 | .initialise = blk_connect, |
62d23efa AL |
855 | .disconnect = blk_disconnect, |
856 | .event = blk_event, | |
857 | .free = blk_free, | |
858 | }; |