2 * xen paravirt block device backend
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.
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.
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/>.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
37 #include <xen/io/xenbus.h>
40 #include "block_int.h"
41 #include "qemu-char.h"
42 #include "xen_blkif.h"
43 #include "xen_backend.h"
46 /* ------------------------------------------------------------- */
48 static int syncwrite = 0;
49 static int batch_maps = 0;
51 static int max_requests = 32;
52 static int use_aio = 1;
54 /* ------------------------------------------------------------- */
56 #define BLOCK_SIZE 512
57 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
70 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
71 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
73 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
80 struct XenBlkDev *blkdev;
81 QLIST_ENTRY(ioreq) list;
85 struct XenDevice xendev; /* must be first */
91 const char *fileproto;
98 blkif_back_rings_t rings;
103 QLIST_HEAD(inflight_head, ioreq) inflight;
104 QLIST_HEAD(finished_head, ioreq) finished;
105 QLIST_HEAD(freelist_head, ioreq) freelist;
107 int requests_inflight;
108 int requests_finished;
110 /* qemu block driver */
112 BlockDriverState *bs;
116 /* ------------------------------------------------------------- */
118 static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
120 struct ioreq *ioreq = NULL;
122 if (QLIST_EMPTY(&blkdev->freelist)) {
123 if (blkdev->requests_total >= max_requests)
125 /* allocate new struct */
126 ioreq = qemu_mallocz(sizeof(*ioreq));
127 ioreq->blkdev = blkdev;
128 blkdev->requests_total++;
129 qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
131 /* get one from freelist */
132 ioreq = QLIST_FIRST(&blkdev->freelist);
133 QLIST_REMOVE(ioreq, list);
134 qemu_iovec_reset(&ioreq->v);
136 QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
137 blkdev->requests_inflight++;
143 static void ioreq_finish(struct ioreq *ioreq)
145 struct XenBlkDev *blkdev = ioreq->blkdev;
147 QLIST_REMOVE(ioreq, list);
148 QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
149 blkdev->requests_inflight--;
150 blkdev->requests_finished++;
153 static void ioreq_release(struct ioreq *ioreq)
155 struct XenBlkDev *blkdev = ioreq->blkdev;
157 QLIST_REMOVE(ioreq, list);
158 memset(ioreq, 0, sizeof(*ioreq));
159 ioreq->blkdev = blkdev;
160 QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
161 blkdev->requests_finished--;
165 * translate request into iovec + start offset
166 * do sanity checks along the way
168 static int ioreq_parse(struct ioreq *ioreq)
170 struct XenBlkDev *blkdev = ioreq->blkdev;
175 xen_be_printf(&blkdev->xendev, 3,
176 "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
177 ioreq->req.operation, ioreq->req.nr_segments,
178 ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
179 switch (ioreq->req.operation) {
181 ioreq->prot = PROT_WRITE; /* to memory */
183 case BLKIF_OP_WRITE_BARRIER:
184 if (!ioreq->req.nr_segments) {
189 ioreq->presync = ioreq->postsync = 1;
192 ioreq->prot = PROT_READ; /* from memory */
197 xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
198 ioreq->req.operation);
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");
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");
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");
217 if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
218 xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
222 ioreq->domids[i] = blkdev->xendev.dom;
223 ioreq->refs[i] = ioreq->req.seg[i].gref;
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);
229 if (ioreq->start + ioreq->v.size > blkdev->file_size) {
230 xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
236 ioreq->status = BLKIF_RSP_ERROR;
240 static void ioreq_unmap(struct ioreq *ioreq)
242 int gnt = ioreq->blkdev->xendev.gnttabdev;
245 if (ioreq->v.niov == 0)
250 if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->v.niov) != 0)
251 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
253 ioreq->blkdev->cnt_map -= ioreq->v.niov;
256 for (i = 0; i < ioreq->v.niov; i++) {
259 if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0)
260 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
262 ioreq->blkdev->cnt_map--;
263 ioreq->page[i] = NULL;
268 static int ioreq_map(struct ioreq *ioreq)
270 int gnt = ioreq->blkdev->xendev.gnttabdev;
273 if (ioreq->v.niov == 0)
276 ioreq->pages = xc_gnttab_map_grant_refs
277 (gnt, ioreq->v.niov, ioreq->domids, ioreq->refs, ioreq->prot);
278 if (ioreq->pages == NULL) {
279 xen_be_printf(&ioreq->blkdev->xendev, 0,
280 "can't map %d grant refs (%s, %d maps)\n",
281 ioreq->v.niov, strerror(errno), ioreq->blkdev->cnt_map);
284 for (i = 0; i < ioreq->v.niov; i++)
285 ioreq->v.iov[i].iov_base = ioreq->pages + i * XC_PAGE_SIZE +
286 (uintptr_t)ioreq->v.iov[i].iov_base;
287 ioreq->blkdev->cnt_map += ioreq->v.niov;
289 for (i = 0; i < ioreq->v.niov; i++) {
290 ioreq->page[i] = xc_gnttab_map_grant_ref
291 (gnt, ioreq->domids[i], ioreq->refs[i], ioreq->prot);
292 if (ioreq->page[i] == NULL) {
293 xen_be_printf(&ioreq->blkdev->xendev, 0,
294 "can't map grant ref %d (%s, %d maps)\n",
295 ioreq->refs[i], strerror(errno), ioreq->blkdev->cnt_map);
299 ioreq->v.iov[i].iov_base = ioreq->page[i] + (uintptr_t)ioreq->v.iov[i].iov_base;
300 ioreq->blkdev->cnt_map++;
306 static int ioreq_runio_qemu_sync(struct ioreq *ioreq)
308 struct XenBlkDev *blkdev = ioreq->blkdev;
312 if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1)
315 bdrv_flush(blkdev->bs);
317 switch (ioreq->req.operation) {
320 for (i = 0; i < ioreq->v.niov; i++) {
321 rc = bdrv_read(blkdev->bs, pos / BLOCK_SIZE,
322 ioreq->v.iov[i].iov_base,
323 ioreq->v.iov[i].iov_len / BLOCK_SIZE);
325 xen_be_printf(&blkdev->xendev, 0, "rd I/O error (%p, len %zd)\n",
326 ioreq->v.iov[i].iov_base,
327 ioreq->v.iov[i].iov_len);
330 len += ioreq->v.iov[i].iov_len;
331 pos += ioreq->v.iov[i].iov_len;
335 case BLKIF_OP_WRITE_BARRIER:
336 if (!ioreq->req.nr_segments)
339 for (i = 0; i < ioreq->v.niov; i++) {
340 rc = bdrv_write(blkdev->bs, pos / BLOCK_SIZE,
341 ioreq->v.iov[i].iov_base,
342 ioreq->v.iov[i].iov_len / BLOCK_SIZE);
344 xen_be_printf(&blkdev->xendev, 0, "wr I/O error (%p, len %zd)\n",
345 ioreq->v.iov[i].iov_base,
346 ioreq->v.iov[i].iov_len);
349 len += ioreq->v.iov[i].iov_len;
350 pos += ioreq->v.iov[i].iov_len;
354 /* unknown operation (shouldn't happen -- parse catches this) */
359 bdrv_flush(blkdev->bs);
360 ioreq->status = BLKIF_RSP_OKAY;
367 ioreq->status = BLKIF_RSP_ERROR;
371 static void qemu_aio_complete(void *opaque, int ret)
373 struct ioreq *ioreq = opaque;
376 xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
377 ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
381 ioreq->aio_inflight--;
382 if (ioreq->aio_inflight > 0)
385 ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
388 qemu_bh_schedule(ioreq->blkdev->bh);
391 static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
393 struct XenBlkDev *blkdev = ioreq->blkdev;
395 if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1)
398 ioreq->aio_inflight++;
400 bdrv_flush(blkdev->bs); /* FIXME: aio_flush() ??? */
402 switch (ioreq->req.operation) {
404 ioreq->aio_inflight++;
405 bdrv_aio_readv(blkdev->bs, ioreq->start / BLOCK_SIZE,
406 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
407 qemu_aio_complete, ioreq);
410 case BLKIF_OP_WRITE_BARRIER:
411 if (!ioreq->req.nr_segments)
413 ioreq->aio_inflight++;
414 bdrv_aio_writev(blkdev->bs, ioreq->start / BLOCK_SIZE,
415 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
416 qemu_aio_complete, ioreq);
419 /* unknown operation (shouldn't happen -- parse catches this) */
424 bdrv_flush(blkdev->bs); /* FIXME: aio_flush() ??? */
425 qemu_aio_complete(ioreq, 0);
430 ioreq->status = BLKIF_RSP_ERROR;
434 static int blk_send_response_one(struct ioreq *ioreq)
436 struct XenBlkDev *blkdev = ioreq->blkdev;
438 int have_requests = 0;
439 blkif_response_t resp;
442 resp.id = ioreq->req.id;
443 resp.operation = ioreq->req.operation;
444 resp.status = ioreq->status;
446 /* Place on the response ring for the relevant domain. */
447 switch (blkdev->protocol) {
448 case BLKIF_PROTOCOL_NATIVE:
449 dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
451 case BLKIF_PROTOCOL_X86_32:
452 dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
453 blkdev->rings.x86_32_part.rsp_prod_pvt);
455 case BLKIF_PROTOCOL_X86_64:
456 dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
457 blkdev->rings.x86_64_part.rsp_prod_pvt);
462 memcpy(dst, &resp, sizeof(resp));
463 blkdev->rings.common.rsp_prod_pvt++;
465 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
466 if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
468 * Tail check for pending requests. Allows frontend to avoid
469 * notifications if requests are already in flight (lower
470 * overheads and promotes batching).
472 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
473 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
482 /* walk finished list, send outstanding responses, free requests */
483 static void blk_send_response_all(struct XenBlkDev *blkdev)
488 while (!QLIST_EMPTY(&blkdev->finished)) {
489 ioreq = QLIST_FIRST(&blkdev->finished);
490 send_notify += blk_send_response_one(ioreq);
491 ioreq_release(ioreq);
494 xen_be_send_notify(&blkdev->xendev);
497 static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
499 switch (blkdev->protocol) {
500 case BLKIF_PROTOCOL_NATIVE:
501 memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
504 case BLKIF_PROTOCOL_X86_32:
505 blkif_get_x86_32_req(&ioreq->req,
506 RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
508 case BLKIF_PROTOCOL_X86_64:
509 blkif_get_x86_64_req(&ioreq->req,
510 RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
516 static void blk_handle_requests(struct XenBlkDev *blkdev)
521 blkdev->more_work = 0;
523 rc = blkdev->rings.common.req_cons;
524 rp = blkdev->rings.common.sring->req_prod;
525 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
528 blk_send_response_all(blkdev);
530 /* pull request from ring */
531 if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc))
533 ioreq = ioreq_start(blkdev);
538 blk_get_request(blkdev, ioreq, rc);
539 blkdev->rings.common.req_cons = ++rc;
542 if (ioreq_parse(ioreq) != 0) {
543 if (blk_send_response_one(ioreq))
544 xen_be_send_notify(&blkdev->xendev);
545 ioreq_release(ioreq);
550 /* run i/o in aio mode */
551 ioreq_runio_qemu_aio(ioreq);
553 /* run i/o in sync mode */
554 ioreq_runio_qemu_sync(ioreq);
558 blk_send_response_all(blkdev);
560 if (blkdev->more_work && blkdev->requests_inflight < max_requests)
561 qemu_bh_schedule(blkdev->bh);
564 /* ------------------------------------------------------------- */
566 static void blk_bh(void *opaque)
568 struct XenBlkDev *blkdev = opaque;
569 blk_handle_requests(blkdev);
572 static void blk_alloc(struct XenDevice *xendev)
574 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
576 QLIST_INIT(&blkdev->inflight);
577 QLIST_INIT(&blkdev->finished);
578 QLIST_INIT(&blkdev->freelist);
579 blkdev->bh = qemu_bh_new(blk_bh, blkdev);
580 if (xen_mode != XEN_EMULATE)
584 static int blk_init(struct XenDevice *xendev)
586 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
587 int index, qflags, have_barriers, info = 0;
590 /* read xenstore entries */
591 if (blkdev->params == NULL) {
592 blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
593 h = strchr(blkdev->params, ':');
595 blkdev->fileproto = blkdev->params;
596 blkdev->filename = h+1;
599 blkdev->fileproto = "<unset>";
600 blkdev->filename = blkdev->params;
603 if (blkdev->mode == NULL)
604 blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
605 if (blkdev->type == NULL)
606 blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
607 if (blkdev->dev == NULL)
608 blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
609 if (blkdev->devtype == NULL)
610 blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
612 /* do we have all we need? */
613 if (blkdev->params == NULL ||
614 blkdev->mode == NULL ||
615 blkdev->type == NULL ||
620 if (strcmp(blkdev->mode, "w") == 0) {
621 qflags = BDRV_O_RDWR;
624 info |= VDISK_READONLY;
628 if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom"))
631 /* init qemu block driver */
632 index = (blkdev->xendev.dev - 202 * 256) / 16;
633 blkdev->dinfo = drive_get(IF_XEN, 0, index);
634 if (!blkdev->dinfo) {
635 /* setup via xenbus -> create new block driver instance */
636 xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
637 blkdev->bs = bdrv_new(blkdev->dev);
638 if (bdrv_open(blkdev->bs, blkdev->filename, qflags,
639 bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) {
640 bdrv_delete(blkdev->bs);
644 /* setup via qemu cmdline -> already setup for us */
645 xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
646 blkdev->bs = blkdev->dinfo->bdrv;
648 blkdev->file_blk = BLOCK_SIZE;
649 blkdev->file_size = bdrv_getlength(blkdev->bs);
650 if (blkdev->file_size < 0) {
651 xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
652 (int)blkdev->file_size, strerror(-blkdev->file_size),
653 blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
654 blkdev->file_size = 0;
656 have_barriers = blkdev->bs->drv && blkdev->bs->drv->bdrv_flush ? 1 : 0;
658 xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
659 " size %" PRId64 " (%" PRId64 " MB)\n",
660 blkdev->type, blkdev->fileproto, blkdev->filename,
661 blkdev->file_size, blkdev->file_size >> 20);
664 xenstore_write_be_int(&blkdev->xendev, "feature-barrier", have_barriers);
665 xenstore_write_be_int(&blkdev->xendev, "info", info);
666 xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
667 xenstore_write_be_int(&blkdev->xendev, "sectors",
668 blkdev->file_size / blkdev->file_blk);
672 static int blk_connect(struct XenDevice *xendev)
674 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
676 if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1)
678 if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
679 &blkdev->xendev.remote_port) == -1)
682 blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
683 if (blkdev->xendev.protocol) {
684 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0)
685 blkdev->protocol = BLKIF_PROTOCOL_X86_32;
686 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0)
687 blkdev->protocol = BLKIF_PROTOCOL_X86_64;
690 blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
693 PROT_READ | PROT_WRITE);
698 switch (blkdev->protocol) {
699 case BLKIF_PROTOCOL_NATIVE:
701 blkif_sring_t *sring_native = blkdev->sring;
702 BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
705 case BLKIF_PROTOCOL_X86_32:
707 blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
709 BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
712 case BLKIF_PROTOCOL_X86_64:
714 blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
716 BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
721 xen_be_bind_evtchn(&blkdev->xendev);
723 xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
724 "remote port %d, local port %d\n",
725 blkdev->xendev.protocol, blkdev->ring_ref,
726 blkdev->xendev.remote_port, blkdev->xendev.local_port);
730 static void blk_disconnect(struct XenDevice *xendev)
732 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
735 if (!blkdev->dinfo) {
736 /* close/delete only if we created it ourself */
737 bdrv_close(blkdev->bs);
738 bdrv_delete(blkdev->bs);
742 xen_be_unbind_evtchn(&blkdev->xendev);
745 xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
747 blkdev->sring = NULL;
751 static int blk_free(struct XenDevice *xendev)
753 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
756 while (!QLIST_EMPTY(&blkdev->freelist)) {
757 ioreq = QLIST_FIRST(&blkdev->freelist);
758 QLIST_REMOVE(ioreq, list);
759 qemu_iovec_destroy(&ioreq->v);
763 qemu_free(blkdev->params);
764 qemu_free(blkdev->mode);
765 qemu_free(blkdev->type);
766 qemu_free(blkdev->dev);
767 qemu_free(blkdev->devtype);
768 qemu_bh_delete(blkdev->bh);
772 static void blk_event(struct XenDevice *xendev)
774 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
776 qemu_bh_schedule(blkdev->bh);
779 struct XenDevOps xen_blkdev_ops = {
780 .size = sizeof(struct XenBlkDev),
781 .flags = DEVOPS_FLAG_NEED_GNTDEV,
784 .connect = blk_connect,
785 .disconnect = blk_disconnect,