Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20150212' into staging
[qemu.git] / hw / block / xen_disk.c
blob21842a01e7123cabc21f13625a355fe14df65927
1 /*
2 * xen paravirt block device backend
4 * (c) Gerd Hoffmann <kraxel@redhat.com>
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/>.
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.
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>
38 #include "hw/hw.h"
39 #include "hw/xen/xen_backend.h"
40 #include "xen_blkif.h"
41 #include "sysemu/blockdev.h"
42 #include "sysemu/block-backend.h"
44 /* ------------------------------------------------------------- */
46 static int batch_maps = 0;
48 static int max_requests = 32;
50 /* ------------------------------------------------------------- */
52 #define BLOCK_SIZE 512
53 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
55 struct PersistentGrant {
56 void *page;
57 struct XenBlkDev *blkdev;
60 typedef struct PersistentGrant PersistentGrant;
62 struct PersistentRegion {
63 void *addr;
64 int num;
67 typedef struct PersistentRegion PersistentRegion;
69 struct ioreq {
70 blkif_request_t req;
71 int16_t status;
73 /* parsed request */
74 off_t start;
75 QEMUIOVector v;
76 int presync;
77 int postsync;
78 uint8_t mapped;
80 /* grant mapping */
81 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
82 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
83 int prot;
84 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
85 void *pages;
86 int num_unmap;
88 /* aio status */
89 int aio_inflight;
90 int aio_errors;
92 struct XenBlkDev *blkdev;
93 QLIST_ENTRY(ioreq) list;
94 BlockAcctCookie acct;
97 struct XenBlkDev {
98 struct XenDevice xendev; /* must be first */
99 char *params;
100 char *mode;
101 char *type;
102 char *dev;
103 char *devtype;
104 bool directiosafe;
105 const char *fileproto;
106 const char *filename;
107 int ring_ref;
108 void *sring;
109 int64_t file_blk;
110 int64_t file_size;
111 int protocol;
112 blkif_back_rings_t rings;
113 int more_work;
114 int cnt_map;
116 /* request lists */
117 QLIST_HEAD(inflight_head, ioreq) inflight;
118 QLIST_HEAD(finished_head, ioreq) finished;
119 QLIST_HEAD(freelist_head, ioreq) freelist;
120 int requests_total;
121 int requests_inflight;
122 int requests_finished;
124 /* Persistent grants extension */
125 gboolean feature_discard;
126 gboolean feature_persistent;
127 GTree *persistent_gnts;
128 GSList *persistent_regions;
129 unsigned int persistent_gnt_count;
130 unsigned int max_grants;
132 /* qemu block driver */
133 DriveInfo *dinfo;
134 BlockBackend *blk;
135 QEMUBH *bh;
138 /* ------------------------------------------------------------- */
140 static void ioreq_reset(struct ioreq *ioreq)
142 memset(&ioreq->req, 0, sizeof(ioreq->req));
143 ioreq->status = 0;
144 ioreq->start = 0;
145 ioreq->presync = 0;
146 ioreq->postsync = 0;
147 ioreq->mapped = 0;
149 memset(ioreq->domids, 0, sizeof(ioreq->domids));
150 memset(ioreq->refs, 0, sizeof(ioreq->refs));
151 ioreq->prot = 0;
152 memset(ioreq->page, 0, sizeof(ioreq->page));
153 ioreq->pages = NULL;
155 ioreq->aio_inflight = 0;
156 ioreq->aio_errors = 0;
158 ioreq->blkdev = NULL;
159 memset(&ioreq->list, 0, sizeof(ioreq->list));
160 memset(&ioreq->acct, 0, sizeof(ioreq->acct));
162 qemu_iovec_reset(&ioreq->v);
165 static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
167 uint ua = GPOINTER_TO_UINT(a);
168 uint ub = GPOINTER_TO_UINT(b);
169 return (ua > ub) - (ua < ub);
172 static void destroy_grant(gpointer pgnt)
174 PersistentGrant *grant = pgnt;
175 XenGnttab gnt = grant->blkdev->xendev.gnttabdev;
177 if (xc_gnttab_munmap(gnt, grant->page, 1) != 0) {
178 xen_be_printf(&grant->blkdev->xendev, 0,
179 "xc_gnttab_munmap failed: %s\n",
180 strerror(errno));
182 grant->blkdev->persistent_gnt_count--;
183 xen_be_printf(&grant->blkdev->xendev, 3,
184 "unmapped grant %p\n", grant->page);
185 g_free(grant);
188 static void remove_persistent_region(gpointer data, gpointer dev)
190 PersistentRegion *region = data;
191 struct XenBlkDev *blkdev = dev;
192 XenGnttab gnt = blkdev->xendev.gnttabdev;
194 if (xc_gnttab_munmap(gnt, region->addr, region->num) != 0) {
195 xen_be_printf(&blkdev->xendev, 0,
196 "xc_gnttab_munmap region %p failed: %s\n",
197 region->addr, strerror(errno));
199 xen_be_printf(&blkdev->xendev, 3,
200 "unmapped grant region %p with %d pages\n",
201 region->addr, region->num);
202 g_free(region);
205 static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
207 struct ioreq *ioreq = NULL;
209 if (QLIST_EMPTY(&blkdev->freelist)) {
210 if (blkdev->requests_total >= max_requests) {
211 goto out;
213 /* allocate new struct */
214 ioreq = g_malloc0(sizeof(*ioreq));
215 ioreq->blkdev = blkdev;
216 blkdev->requests_total++;
217 qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
218 } else {
219 /* get one from freelist */
220 ioreq = QLIST_FIRST(&blkdev->freelist);
221 QLIST_REMOVE(ioreq, list);
223 QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
224 blkdev->requests_inflight++;
226 out:
227 return ioreq;
230 static void ioreq_finish(struct ioreq *ioreq)
232 struct XenBlkDev *blkdev = ioreq->blkdev;
234 QLIST_REMOVE(ioreq, list);
235 QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
236 blkdev->requests_inflight--;
237 blkdev->requests_finished++;
240 static void ioreq_release(struct ioreq *ioreq, bool finish)
242 struct XenBlkDev *blkdev = ioreq->blkdev;
244 QLIST_REMOVE(ioreq, list);
245 ioreq_reset(ioreq);
246 ioreq->blkdev = blkdev;
247 QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
248 if (finish) {
249 blkdev->requests_finished--;
250 } else {
251 blkdev->requests_inflight--;
256 * translate request into iovec + start offset
257 * do sanity checks along the way
259 static int ioreq_parse(struct ioreq *ioreq)
261 struct XenBlkDev *blkdev = ioreq->blkdev;
262 uintptr_t mem;
263 size_t len;
264 int i;
266 xen_be_printf(&blkdev->xendev, 3,
267 "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
268 ioreq->req.operation, ioreq->req.nr_segments,
269 ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
270 switch (ioreq->req.operation) {
271 case BLKIF_OP_READ:
272 ioreq->prot = PROT_WRITE; /* to memory */
273 break;
274 case BLKIF_OP_FLUSH_DISKCACHE:
275 ioreq->presync = 1;
276 if (!ioreq->req.nr_segments) {
277 return 0;
279 /* fall through */
280 case BLKIF_OP_WRITE:
281 ioreq->prot = PROT_READ; /* from memory */
282 break;
283 case BLKIF_OP_DISCARD:
284 return 0;
285 default:
286 xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
287 ioreq->req.operation);
288 goto err;
291 if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') {
292 xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n");
293 goto err;
296 ioreq->start = ioreq->req.sector_number * blkdev->file_blk;
297 for (i = 0; i < ioreq->req.nr_segments; i++) {
298 if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
299 xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n");
300 goto err;
302 if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) {
303 xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n");
304 goto err;
306 if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
307 xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
308 goto err;
311 ioreq->domids[i] = blkdev->xendev.dom;
312 ioreq->refs[i] = ioreq->req.seg[i].gref;
314 mem = ioreq->req.seg[i].first_sect * blkdev->file_blk;
315 len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk;
316 qemu_iovec_add(&ioreq->v, (void*)mem, len);
318 if (ioreq->start + ioreq->v.size > blkdev->file_size) {
319 xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
320 goto err;
322 return 0;
324 err:
325 ioreq->status = BLKIF_RSP_ERROR;
326 return -1;
329 static void ioreq_unmap(struct ioreq *ioreq)
331 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
332 int i;
334 if (ioreq->num_unmap == 0 || ioreq->mapped == 0) {
335 return;
337 if (batch_maps) {
338 if (!ioreq->pages) {
339 return;
341 if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
342 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
343 strerror(errno));
345 ioreq->blkdev->cnt_map -= ioreq->num_unmap;
346 ioreq->pages = NULL;
347 } else {
348 for (i = 0; i < ioreq->num_unmap; i++) {
349 if (!ioreq->page[i]) {
350 continue;
352 if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
353 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
354 strerror(errno));
356 ioreq->blkdev->cnt_map--;
357 ioreq->page[i] = NULL;
360 ioreq->mapped = 0;
363 static int ioreq_map(struct ioreq *ioreq)
365 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
366 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
367 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
368 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
369 int i, j, new_maps = 0;
370 PersistentGrant *grant;
371 PersistentRegion *region;
372 /* domids and refs variables will contain the information necessary
373 * to map the grants that are needed to fulfill this request.
375 * After mapping the needed grants, the page array will contain the
376 * memory address of each granted page in the order specified in ioreq
377 * (disregarding if it's a persistent grant or not).
380 if (ioreq->v.niov == 0 || ioreq->mapped == 1) {
381 return 0;
383 if (ioreq->blkdev->feature_persistent) {
384 for (i = 0; i < ioreq->v.niov; i++) {
385 grant = g_tree_lookup(ioreq->blkdev->persistent_gnts,
386 GUINT_TO_POINTER(ioreq->refs[i]));
388 if (grant != NULL) {
389 page[i] = grant->page;
390 xen_be_printf(&ioreq->blkdev->xendev, 3,
391 "using persistent-grant %" PRIu32 "\n",
392 ioreq->refs[i]);
393 } else {
394 /* Add the grant to the list of grants that
395 * should be mapped
397 domids[new_maps] = ioreq->domids[i];
398 refs[new_maps] = ioreq->refs[i];
399 page[i] = NULL;
400 new_maps++;
403 /* Set the protection to RW, since grants may be reused later
404 * with a different protection than the one needed for this request
406 ioreq->prot = PROT_WRITE | PROT_READ;
407 } else {
408 /* All grants in the request should be mapped */
409 memcpy(refs, ioreq->refs, sizeof(refs));
410 memcpy(domids, ioreq->domids, sizeof(domids));
411 memset(page, 0, sizeof(page));
412 new_maps = ioreq->v.niov;
415 if (batch_maps && new_maps) {
416 ioreq->pages = xc_gnttab_map_grant_refs
417 (gnt, new_maps, domids, refs, ioreq->prot);
418 if (ioreq->pages == NULL) {
419 xen_be_printf(&ioreq->blkdev->xendev, 0,
420 "can't map %d grant refs (%s, %d maps)\n",
421 new_maps, strerror(errno), ioreq->blkdev->cnt_map);
422 return -1;
424 for (i = 0, j = 0; i < ioreq->v.niov; i++) {
425 if (page[i] == NULL) {
426 page[i] = ioreq->pages + (j++) * XC_PAGE_SIZE;
429 ioreq->blkdev->cnt_map += new_maps;
430 } else if (new_maps) {
431 for (i = 0; i < new_maps; i++) {
432 ioreq->page[i] = xc_gnttab_map_grant_ref
433 (gnt, domids[i], refs[i], ioreq->prot);
434 if (ioreq->page[i] == NULL) {
435 xen_be_printf(&ioreq->blkdev->xendev, 0,
436 "can't map grant ref %d (%s, %d maps)\n",
437 refs[i], strerror(errno), ioreq->blkdev->cnt_map);
438 ioreq->mapped = 1;
439 ioreq_unmap(ioreq);
440 return -1;
442 ioreq->blkdev->cnt_map++;
444 for (i = 0, j = 0; i < ioreq->v.niov; i++) {
445 if (page[i] == NULL) {
446 page[i] = ioreq->page[j++];
450 if (ioreq->blkdev->feature_persistent && new_maps != 0 &&
451 (!batch_maps || (ioreq->blkdev->persistent_gnt_count + new_maps <=
452 ioreq->blkdev->max_grants))) {
454 * If we are using persistent grants and batch mappings only
455 * add the new maps to the list of persistent grants if the whole
456 * area can be persistently mapped.
458 if (batch_maps) {
459 region = g_malloc0(sizeof(*region));
460 region->addr = ioreq->pages;
461 region->num = new_maps;
462 ioreq->blkdev->persistent_regions = g_slist_append(
463 ioreq->blkdev->persistent_regions,
464 region);
466 while ((ioreq->blkdev->persistent_gnt_count < ioreq->blkdev->max_grants)
467 && new_maps) {
468 /* Go through the list of newly mapped grants and add as many
469 * as possible to the list of persistently mapped grants.
471 * Since we start at the end of ioreq->page(s), we only need
472 * to decrease new_maps to prevent this granted pages from
473 * being unmapped in ioreq_unmap.
475 grant = g_malloc0(sizeof(*grant));
476 new_maps--;
477 if (batch_maps) {
478 grant->page = ioreq->pages + (new_maps) * XC_PAGE_SIZE;
479 } else {
480 grant->page = ioreq->page[new_maps];
482 grant->blkdev = ioreq->blkdev;
483 xen_be_printf(&ioreq->blkdev->xendev, 3,
484 "adding grant %" PRIu32 " page: %p\n",
485 refs[new_maps], grant->page);
486 g_tree_insert(ioreq->blkdev->persistent_gnts,
487 GUINT_TO_POINTER(refs[new_maps]),
488 grant);
489 ioreq->blkdev->persistent_gnt_count++;
491 assert(!batch_maps || new_maps == 0);
493 for (i = 0; i < ioreq->v.niov; i++) {
494 ioreq->v.iov[i].iov_base += (uintptr_t)page[i];
496 ioreq->mapped = 1;
497 ioreq->num_unmap = new_maps;
498 return 0;
501 static int ioreq_runio_qemu_aio(struct ioreq *ioreq);
503 static void qemu_aio_complete(void *opaque, int ret)
505 struct ioreq *ioreq = opaque;
507 if (ret != 0) {
508 xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
509 ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
510 ioreq->aio_errors++;
513 ioreq->aio_inflight--;
514 if (ioreq->presync) {
515 ioreq->presync = 0;
516 ioreq_runio_qemu_aio(ioreq);
517 return;
519 if (ioreq->aio_inflight > 0) {
520 return;
522 if (ioreq->postsync) {
523 ioreq->postsync = 0;
524 ioreq->aio_inflight++;
525 blk_aio_flush(ioreq->blkdev->blk, qemu_aio_complete, ioreq);
526 return;
529 ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
530 ioreq_unmap(ioreq);
531 ioreq_finish(ioreq);
532 switch (ioreq->req.operation) {
533 case BLKIF_OP_WRITE:
534 case BLKIF_OP_FLUSH_DISKCACHE:
535 if (!ioreq->req.nr_segments) {
536 break;
538 case BLKIF_OP_READ:
539 block_acct_done(blk_get_stats(ioreq->blkdev->blk), &ioreq->acct);
540 break;
541 case BLKIF_OP_DISCARD:
542 default:
543 break;
545 qemu_bh_schedule(ioreq->blkdev->bh);
548 static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
550 struct XenBlkDev *blkdev = ioreq->blkdev;
552 if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) {
553 goto err_no_map;
556 ioreq->aio_inflight++;
557 if (ioreq->presync) {
558 blk_aio_flush(ioreq->blkdev->blk, qemu_aio_complete, ioreq);
559 return 0;
562 switch (ioreq->req.operation) {
563 case BLKIF_OP_READ:
564 block_acct_start(blk_get_stats(blkdev->blk), &ioreq->acct,
565 ioreq->v.size, BLOCK_ACCT_READ);
566 ioreq->aio_inflight++;
567 blk_aio_readv(blkdev->blk, ioreq->start / BLOCK_SIZE,
568 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
569 qemu_aio_complete, ioreq);
570 break;
571 case BLKIF_OP_WRITE:
572 case BLKIF_OP_FLUSH_DISKCACHE:
573 if (!ioreq->req.nr_segments) {
574 break;
577 block_acct_start(blk_get_stats(blkdev->blk), &ioreq->acct,
578 ioreq->v.size, BLOCK_ACCT_WRITE);
579 ioreq->aio_inflight++;
580 blk_aio_writev(blkdev->blk, ioreq->start / BLOCK_SIZE,
581 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
582 qemu_aio_complete, ioreq);
583 break;
584 case BLKIF_OP_DISCARD:
586 struct blkif_request_discard *discard_req = (void *)&ioreq->req;
587 ioreq->aio_inflight++;
588 blk_aio_discard(blkdev->blk,
589 discard_req->sector_number, discard_req->nr_sectors,
590 qemu_aio_complete, ioreq);
591 break;
593 default:
594 /* unknown operation (shouldn't happen -- parse catches this) */
595 goto err;
598 qemu_aio_complete(ioreq, 0);
600 return 0;
602 err:
603 ioreq_unmap(ioreq);
604 err_no_map:
605 ioreq_finish(ioreq);
606 ioreq->status = BLKIF_RSP_ERROR;
607 return -1;
610 static int blk_send_response_one(struct ioreq *ioreq)
612 struct XenBlkDev *blkdev = ioreq->blkdev;
613 int send_notify = 0;
614 int have_requests = 0;
615 blkif_response_t resp;
616 void *dst;
618 resp.id = ioreq->req.id;
619 resp.operation = ioreq->req.operation;
620 resp.status = ioreq->status;
622 /* Place on the response ring for the relevant domain. */
623 switch (blkdev->protocol) {
624 case BLKIF_PROTOCOL_NATIVE:
625 dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
626 break;
627 case BLKIF_PROTOCOL_X86_32:
628 dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
629 blkdev->rings.x86_32_part.rsp_prod_pvt);
630 break;
631 case BLKIF_PROTOCOL_X86_64:
632 dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
633 blkdev->rings.x86_64_part.rsp_prod_pvt);
634 break;
635 default:
636 dst = NULL;
637 return 0;
639 memcpy(dst, &resp, sizeof(resp));
640 blkdev->rings.common.rsp_prod_pvt++;
642 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
643 if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
645 * Tail check for pending requests. Allows frontend to avoid
646 * notifications if requests are already in flight (lower
647 * overheads and promotes batching).
649 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
650 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
651 have_requests = 1;
654 if (have_requests) {
655 blkdev->more_work++;
657 return send_notify;
660 /* walk finished list, send outstanding responses, free requests */
661 static void blk_send_response_all(struct XenBlkDev *blkdev)
663 struct ioreq *ioreq;
664 int send_notify = 0;
666 while (!QLIST_EMPTY(&blkdev->finished)) {
667 ioreq = QLIST_FIRST(&blkdev->finished);
668 send_notify += blk_send_response_one(ioreq);
669 ioreq_release(ioreq, true);
671 if (send_notify) {
672 xen_be_send_notify(&blkdev->xendev);
676 static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
678 switch (blkdev->protocol) {
679 case BLKIF_PROTOCOL_NATIVE:
680 memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
681 sizeof(ioreq->req));
682 break;
683 case BLKIF_PROTOCOL_X86_32:
684 blkif_get_x86_32_req(&ioreq->req,
685 RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
686 break;
687 case BLKIF_PROTOCOL_X86_64:
688 blkif_get_x86_64_req(&ioreq->req,
689 RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
690 break;
692 return 0;
695 static void blk_handle_requests(struct XenBlkDev *blkdev)
697 RING_IDX rc, rp;
698 struct ioreq *ioreq;
700 blkdev->more_work = 0;
702 rc = blkdev->rings.common.req_cons;
703 rp = blkdev->rings.common.sring->req_prod;
704 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
706 blk_send_response_all(blkdev);
707 while (rc != rp) {
708 /* pull request from ring */
709 if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) {
710 break;
712 ioreq = ioreq_start(blkdev);
713 if (ioreq == NULL) {
714 blkdev->more_work++;
715 break;
717 blk_get_request(blkdev, ioreq, rc);
718 blkdev->rings.common.req_cons = ++rc;
720 /* parse them */
721 if (ioreq_parse(ioreq) != 0) {
722 if (blk_send_response_one(ioreq)) {
723 xen_be_send_notify(&blkdev->xendev);
725 ioreq_release(ioreq, false);
726 continue;
729 ioreq_runio_qemu_aio(ioreq);
732 if (blkdev->more_work && blkdev->requests_inflight < max_requests) {
733 qemu_bh_schedule(blkdev->bh);
737 /* ------------------------------------------------------------- */
739 static void blk_bh(void *opaque)
741 struct XenBlkDev *blkdev = opaque;
742 blk_handle_requests(blkdev);
746 * We need to account for the grant allocations requiring contiguous
747 * chunks; the worst case number would be
748 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
749 * but in order to keep things simple just use
750 * 2 * max_req * max_seg.
752 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
754 static void blk_alloc(struct XenDevice *xendev)
756 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
758 QLIST_INIT(&blkdev->inflight);
759 QLIST_INIT(&blkdev->finished);
760 QLIST_INIT(&blkdev->freelist);
761 blkdev->bh = qemu_bh_new(blk_bh, blkdev);
762 if (xen_mode != XEN_EMULATE) {
763 batch_maps = 1;
765 if (xc_gnttab_set_max_grants(xendev->gnttabdev,
766 MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
767 xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
768 strerror(errno));
772 static void blk_parse_discard(struct XenBlkDev *blkdev)
774 int enable;
776 blkdev->feature_discard = true;
778 if (xenstore_read_be_int(&blkdev->xendev, "discard-enable", &enable) == 0) {
779 blkdev->feature_discard = !!enable;
782 if (blkdev->feature_discard) {
783 xenstore_write_be_int(&blkdev->xendev, "feature-discard", 1);
787 static int blk_init(struct XenDevice *xendev)
789 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
790 int info = 0;
791 char *directiosafe = NULL;
793 /* read xenstore entries */
794 if (blkdev->params == NULL) {
795 char *h = NULL;
796 blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
797 if (blkdev->params != NULL) {
798 h = strchr(blkdev->params, ':');
800 if (h != NULL) {
801 blkdev->fileproto = blkdev->params;
802 blkdev->filename = h+1;
803 *h = 0;
804 } else {
805 blkdev->fileproto = "<unset>";
806 blkdev->filename = blkdev->params;
809 if (!strcmp("aio", blkdev->fileproto)) {
810 blkdev->fileproto = "raw";
812 if (blkdev->mode == NULL) {
813 blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
815 if (blkdev->type == NULL) {
816 blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
818 if (blkdev->dev == NULL) {
819 blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
821 if (blkdev->devtype == NULL) {
822 blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
824 directiosafe = xenstore_read_be_str(&blkdev->xendev, "direct-io-safe");
825 blkdev->directiosafe = (directiosafe && atoi(directiosafe));
827 /* do we have all we need? */
828 if (blkdev->params == NULL ||
829 blkdev->mode == NULL ||
830 blkdev->type == NULL ||
831 blkdev->dev == NULL) {
832 goto out_error;
835 /* read-only ? */
836 if (strcmp(blkdev->mode, "w")) {
837 info |= VDISK_READONLY;
840 /* cdrom ? */
841 if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) {
842 info |= VDISK_CDROM;
845 blkdev->file_blk = BLOCK_SIZE;
847 /* fill info
848 * blk_connect supplies sector-size and sectors
850 xenstore_write_be_int(&blkdev->xendev, "feature-flush-cache", 1);
851 xenstore_write_be_int(&blkdev->xendev, "feature-persistent", 1);
852 xenstore_write_be_int(&blkdev->xendev, "info", info);
854 blk_parse_discard(blkdev);
856 g_free(directiosafe);
857 return 0;
859 out_error:
860 g_free(blkdev->params);
861 blkdev->params = NULL;
862 g_free(blkdev->mode);
863 blkdev->mode = NULL;
864 g_free(blkdev->type);
865 blkdev->type = NULL;
866 g_free(blkdev->dev);
867 blkdev->dev = NULL;
868 g_free(blkdev->devtype);
869 blkdev->devtype = NULL;
870 g_free(directiosafe);
871 blkdev->directiosafe = false;
872 return -1;
875 static int blk_connect(struct XenDevice *xendev)
877 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
878 int pers, index, qflags;
879 bool readonly = true;
881 /* read-only ? */
882 if (blkdev->directiosafe) {
883 qflags = BDRV_O_NOCACHE | BDRV_O_NATIVE_AIO;
884 } else {
885 qflags = BDRV_O_CACHE_WB;
887 if (strcmp(blkdev->mode, "w") == 0) {
888 qflags |= BDRV_O_RDWR;
889 readonly = false;
891 if (blkdev->feature_discard) {
892 qflags |= BDRV_O_UNMAP;
895 /* init qemu block driver */
896 index = (blkdev->xendev.dev - 202 * 256) / 16;
897 blkdev->dinfo = drive_get(IF_XEN, 0, index);
898 if (!blkdev->dinfo) {
899 Error *local_err = NULL;
900 BlockBackend *blk;
901 BlockDriver *drv;
902 BlockDriverState *bs;
904 /* setup via xenbus -> create new block driver instance */
905 xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
906 blk = blk_new_with_bs(blkdev->dev, NULL);
907 if (!blk) {
908 return -1;
910 blkdev->blk = blk;
912 bs = blk_bs(blk);
913 drv = bdrv_find_whitelisted_format(blkdev->fileproto, readonly);
914 if (bdrv_open(&bs, blkdev->filename, NULL, NULL, qflags,
915 drv, &local_err) != 0) {
916 xen_be_printf(&blkdev->xendev, 0, "error: %s\n",
917 error_get_pretty(local_err));
918 error_free(local_err);
919 blk_unref(blk);
920 blkdev->blk = NULL;
921 return -1;
923 assert(bs == blk_bs(blk));
924 } else {
925 /* setup via qemu cmdline -> already setup for us */
926 xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
927 blkdev->blk = blk_by_legacy_dinfo(blkdev->dinfo);
928 if (blk_is_read_only(blkdev->blk) && !readonly) {
929 xen_be_printf(&blkdev->xendev, 0, "Unexpected read-only drive");
930 blkdev->blk = NULL;
931 return -1;
933 /* blkdev->blk is not create by us, we get a reference
934 * so we can blk_unref() unconditionally */
935 blk_ref(blkdev->blk);
937 blk_attach_dev_nofail(blkdev->blk, blkdev);
938 blkdev->file_size = blk_getlength(blkdev->blk);
939 if (blkdev->file_size < 0) {
940 xen_be_printf(&blkdev->xendev, 1, "blk_getlength: %d (%s) | drv %s\n",
941 (int)blkdev->file_size, strerror(-blkdev->file_size),
942 bdrv_get_format_name(blk_bs(blkdev->blk)) ?: "-");
943 blkdev->file_size = 0;
946 xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
947 " size %" PRId64 " (%" PRId64 " MB)\n",
948 blkdev->type, blkdev->fileproto, blkdev->filename,
949 blkdev->file_size, blkdev->file_size >> 20);
951 /* Fill in number of sector size and number of sectors */
952 xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
953 xenstore_write_be_int64(&blkdev->xendev, "sectors",
954 blkdev->file_size / blkdev->file_blk);
956 if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) {
957 return -1;
959 if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
960 &blkdev->xendev.remote_port) == -1) {
961 return -1;
963 if (xenstore_read_fe_int(&blkdev->xendev, "feature-persistent", &pers)) {
964 blkdev->feature_persistent = FALSE;
965 } else {
966 blkdev->feature_persistent = !!pers;
969 blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
970 if (blkdev->xendev.protocol) {
971 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
972 blkdev->protocol = BLKIF_PROTOCOL_X86_32;
974 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
975 blkdev->protocol = BLKIF_PROTOCOL_X86_64;
979 blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
980 blkdev->xendev.dom,
981 blkdev->ring_ref,
982 PROT_READ | PROT_WRITE);
983 if (!blkdev->sring) {
984 return -1;
986 blkdev->cnt_map++;
988 switch (blkdev->protocol) {
989 case BLKIF_PROTOCOL_NATIVE:
991 blkif_sring_t *sring_native = blkdev->sring;
992 BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
993 break;
995 case BLKIF_PROTOCOL_X86_32:
997 blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
999 BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
1000 break;
1002 case BLKIF_PROTOCOL_X86_64:
1004 blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
1006 BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
1007 break;
1011 if (blkdev->feature_persistent) {
1012 /* Init persistent grants */
1013 blkdev->max_grants = max_requests * BLKIF_MAX_SEGMENTS_PER_REQUEST;
1014 blkdev->persistent_gnts = g_tree_new_full((GCompareDataFunc)int_cmp,
1015 NULL, NULL,
1016 batch_maps ?
1017 (GDestroyNotify)g_free :
1018 (GDestroyNotify)destroy_grant);
1019 blkdev->persistent_regions = NULL;
1020 blkdev->persistent_gnt_count = 0;
1023 xen_be_bind_evtchn(&blkdev->xendev);
1025 xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
1026 "remote port %d, local port %d\n",
1027 blkdev->xendev.protocol, blkdev->ring_ref,
1028 blkdev->xendev.remote_port, blkdev->xendev.local_port);
1029 return 0;
1032 static void blk_disconnect(struct XenDevice *xendev)
1034 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1036 if (blkdev->blk) {
1037 blk_detach_dev(blkdev->blk, blkdev);
1038 blk_unref(blkdev->blk);
1039 blkdev->blk = NULL;
1041 xen_be_unbind_evtchn(&blkdev->xendev);
1043 if (blkdev->sring) {
1044 xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
1045 blkdev->cnt_map--;
1046 blkdev->sring = NULL;
1050 * Unmap persistent grants before switching to the closed state
1051 * so the frontend can free them.
1053 * In the !batch_maps case g_tree_destroy will take care of unmapping
1054 * the grant, but in the batch_maps case we need to iterate over every
1055 * region in persistent_regions and unmap it.
1057 if (blkdev->feature_persistent) {
1058 g_tree_destroy(blkdev->persistent_gnts);
1059 assert(batch_maps || blkdev->persistent_gnt_count == 0);
1060 if (batch_maps) {
1061 blkdev->persistent_gnt_count = 0;
1062 g_slist_foreach(blkdev->persistent_regions,
1063 (GFunc)remove_persistent_region, blkdev);
1064 g_slist_free(blkdev->persistent_regions);
1066 blkdev->feature_persistent = false;
1070 static int blk_free(struct XenDevice *xendev)
1072 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1073 struct ioreq *ioreq;
1075 if (blkdev->blk || blkdev->sring) {
1076 blk_disconnect(xendev);
1079 while (!QLIST_EMPTY(&blkdev->freelist)) {
1080 ioreq = QLIST_FIRST(&blkdev->freelist);
1081 QLIST_REMOVE(ioreq, list);
1082 qemu_iovec_destroy(&ioreq->v);
1083 g_free(ioreq);
1086 g_free(blkdev->params);
1087 g_free(blkdev->mode);
1088 g_free(blkdev->type);
1089 g_free(blkdev->dev);
1090 g_free(blkdev->devtype);
1091 qemu_bh_delete(blkdev->bh);
1092 return 0;
1095 static void blk_event(struct XenDevice *xendev)
1097 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1099 qemu_bh_schedule(blkdev->bh);
1102 struct XenDevOps xen_blkdev_ops = {
1103 .size = sizeof(struct XenBlkDev),
1104 .flags = DEVOPS_FLAG_NEED_GNTDEV,
1105 .alloc = blk_alloc,
1106 .init = blk_init,
1107 .initialise = blk_connect,
1108 .disconnect = blk_disconnect,
1109 .event = blk_event,
1110 .free = blk_free,