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