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.
32 #include <sys/ioctl.h>
33 #include <sys/types.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
{
57 struct XenBlkDev
*blkdev
;
60 typedef struct PersistentGrant PersistentGrant
;
62 struct PersistentRegion
{
67 typedef struct PersistentRegion PersistentRegion
;
81 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
82 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
84 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
92 struct XenBlkDev
*blkdev
;
93 QLIST_ENTRY(ioreq
) list
;
98 struct XenDevice xendev
; /* must be first */
105 const char *fileproto
;
106 const char *filename
;
112 blkif_back_rings_t rings
;
117 QLIST_HEAD(inflight_head
, ioreq
) inflight
;
118 QLIST_HEAD(finished_head
, ioreq
) finished
;
119 QLIST_HEAD(freelist_head
, ioreq
) freelist
;
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 */
138 /* ------------------------------------------------------------- */
140 static void ioreq_reset(struct ioreq
*ioreq
)
142 memset(&ioreq
->req
, 0, sizeof(ioreq
->req
));
149 memset(ioreq
->domids
, 0, sizeof(ioreq
->domids
));
150 memset(ioreq
->refs
, 0, sizeof(ioreq
->refs
));
152 memset(ioreq
->page
, 0, sizeof(ioreq
->page
));
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",
182 grant
->blkdev
->persistent_gnt_count
--;
183 xen_be_printf(&grant
->blkdev
->xendev
, 3,
184 "unmapped grant %p\n", grant
->page
);
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
);
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
) {
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
);
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
++;
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
);
246 ioreq
->blkdev
= blkdev
;
247 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
249 blkdev
->requests_finished
--;
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
;
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
) {
272 ioreq
->prot
= PROT_WRITE
; /* to memory */
274 case BLKIF_OP_FLUSH_DISKCACHE
:
276 if (!ioreq
->req
.nr_segments
) {
281 ioreq
->prot
= PROT_READ
; /* from memory */
283 case BLKIF_OP_DISCARD
:
286 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
287 ioreq
->req
.operation
);
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");
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");
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");
306 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
307 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
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");
325 ioreq
->status
= BLKIF_RSP_ERROR
;
329 static void ioreq_unmap(struct ioreq
*ioreq
)
331 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
334 if (ioreq
->num_unmap
== 0 || ioreq
->mapped
== 0) {
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",
345 ioreq
->blkdev
->cnt_map
-= ioreq
->num_unmap
;
348 for (i
= 0; i
< ioreq
->num_unmap
; i
++) {
349 if (!ioreq
->page
[i
]) {
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",
356 ioreq
->blkdev
->cnt_map
--;
357 ioreq
->page
[i
] = NULL
;
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) {
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
]));
389 page
[i
] = grant
->page
;
390 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
391 "using persistent-grant %" PRIu32
"\n",
394 /* Add the grant to the list of grants that
397 domids
[new_maps
] = ioreq
->domids
[i
];
398 refs
[new_maps
] = ioreq
->refs
[i
];
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
;
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
);
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
);
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.
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
,
466 while ((ioreq
->blkdev
->persistent_gnt_count
< ioreq
->blkdev
->max_grants
)
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
));
478 grant
->page
= ioreq
->pages
+ (new_maps
) * XC_PAGE_SIZE
;
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
]),
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
];
497 ioreq
->num_unmap
= new_maps
;
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
;
508 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
509 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
513 ioreq
->aio_inflight
--;
514 if (ioreq
->presync
) {
516 ioreq_runio_qemu_aio(ioreq
);
519 if (ioreq
->aio_inflight
> 0) {
522 if (ioreq
->postsync
) {
524 ioreq
->aio_inflight
++;
525 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
529 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
532 switch (ioreq
->req
.operation
) {
534 case BLKIF_OP_FLUSH_DISKCACHE
:
535 if (!ioreq
->req
.nr_segments
) {
539 block_acct_done(blk_get_stats(ioreq
->blkdev
->blk
), &ioreq
->acct
);
541 case BLKIF_OP_DISCARD
:
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) {
556 ioreq
->aio_inflight
++;
557 if (ioreq
->presync
) {
558 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
562 switch (ioreq
->req
.operation
) {
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
);
572 case BLKIF_OP_FLUSH_DISKCACHE
:
573 if (!ioreq
->req
.nr_segments
) {
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
);
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
);
594 /* unknown operation (shouldn't happen -- parse catches this) */
598 qemu_aio_complete(ioreq
, 0);
606 ioreq
->status
= BLKIF_RSP_ERROR
;
610 static int blk_send_response_one(struct ioreq
*ioreq
)
612 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
614 int have_requests
= 0;
615 blkif_response_t resp
;
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
);
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
);
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
);
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
)) {
660 /* walk finished list, send outstanding responses, free requests */
661 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
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);
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
),
683 case BLKIF_PROTOCOL_X86_32
:
684 blkif_get_x86_32_req(&ioreq
->req
,
685 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
687 case BLKIF_PROTOCOL_X86_64
:
688 blkif_get_x86_64_req(&ioreq
->req
,
689 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
695 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
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
);
708 /* pull request from ring */
709 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
712 ioreq
= ioreq_start(blkdev
);
717 blk_get_request(blkdev
, ioreq
, rc
);
718 blkdev
->rings
.common
.req_cons
= ++rc
;
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);
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
) {
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",
772 static void blk_parse_discard(struct XenBlkDev
*blkdev
)
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
);
791 char *directiosafe
= NULL
;
793 /* read xenstore entries */
794 if (blkdev
->params
== NULL
) {
796 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
797 if (blkdev
->params
!= NULL
) {
798 h
= strchr(blkdev
->params
, ':');
801 blkdev
->fileproto
= blkdev
->params
;
802 blkdev
->filename
= h
+1;
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
) {
836 if (strcmp(blkdev
->mode
, "w")) {
837 info
|= VDISK_READONLY
;
841 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
845 blkdev
->file_blk
= BLOCK_SIZE
;
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
);
860 g_free(blkdev
->params
);
861 blkdev
->params
= NULL
;
862 g_free(blkdev
->mode
);
864 g_free(blkdev
->type
);
868 g_free(blkdev
->devtype
);
869 blkdev
->devtype
= NULL
;
870 g_free(directiosafe
);
871 blkdev
->directiosafe
= false;
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;
882 if (blkdev
->directiosafe
) {
883 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
885 qflags
= BDRV_O_CACHE_WB
;
887 if (strcmp(blkdev
->mode
, "w") == 0) {
888 qflags
|= BDRV_O_RDWR
;
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
;
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
);
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
);
923 assert(bs
== blk_bs(blk
));
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");
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) {
959 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
960 &blkdev
->xendev
.remote_port
) == -1) {
963 if (xenstore_read_fe_int(&blkdev
->xendev
, "feature-persistent", &pers
)) {
964 blkdev
->feature_persistent
= FALSE
;
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
,
982 PROT_READ
| PROT_WRITE
);
983 if (!blkdev
->sring
) {
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
);
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
);
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
);
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
,
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
);
1032 static void blk_disconnect(struct XenDevice
*xendev
)
1034 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1037 blk_detach_dev(blkdev
->blk
, blkdev
);
1038 blk_unref(blkdev
->blk
);
1041 xen_be_unbind_evtchn(&blkdev
->xendev
);
1043 if (blkdev
->sring
) {
1044 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
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);
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
);
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
);
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
,
1107 .initialise
= blk_connect
,
1108 .disconnect
= blk_disconnect
,