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.
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
38 #include "hw/xen/xen_backend.h"
39 #include "xen_blkif.h"
40 #include "sysemu/blockdev.h"
41 #include "sysemu/block-backend.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qstring.h"
45 /* ------------------------------------------------------------- */
47 static int batch_maps
= 0;
49 static int max_requests
= 32;
51 /* ------------------------------------------------------------- */
53 #define BLOCK_SIZE 512
54 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
56 struct PersistentGrant
{
58 struct XenBlkDev
*blkdev
;
61 typedef struct PersistentGrant PersistentGrant
;
63 struct PersistentRegion
{
68 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
));
148 memset(ioreq
->domids
, 0, sizeof(ioreq
->domids
));
149 memset(ioreq
->refs
, 0, sizeof(ioreq
->refs
));
151 memset(ioreq
->page
, 0, sizeof(ioreq
->page
));
154 ioreq
->aio_inflight
= 0;
155 ioreq
->aio_errors
= 0;
157 ioreq
->blkdev
= NULL
;
158 memset(&ioreq
->list
, 0, sizeof(ioreq
->list
));
159 memset(&ioreq
->acct
, 0, sizeof(ioreq
->acct
));
161 qemu_iovec_reset(&ioreq
->v
);
164 static gint
int_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
166 uint ua
= GPOINTER_TO_UINT(a
);
167 uint ub
= GPOINTER_TO_UINT(b
);
168 return (ua
> ub
) - (ua
< ub
);
171 static void destroy_grant(gpointer pgnt
)
173 PersistentGrant
*grant
= pgnt
;
174 XenGnttab gnt
= grant
->blkdev
->xendev
.gnttabdev
;
176 if (xc_gnttab_munmap(gnt
, grant
->page
, 1) != 0) {
177 xen_be_printf(&grant
->blkdev
->xendev
, 0,
178 "xc_gnttab_munmap failed: %s\n",
181 grant
->blkdev
->persistent_gnt_count
--;
182 xen_be_printf(&grant
->blkdev
->xendev
, 3,
183 "unmapped grant %p\n", grant
->page
);
187 static void remove_persistent_region(gpointer data
, gpointer dev
)
189 PersistentRegion
*region
= data
;
190 struct XenBlkDev
*blkdev
= dev
;
191 XenGnttab gnt
= blkdev
->xendev
.gnttabdev
;
193 if (xc_gnttab_munmap(gnt
, region
->addr
, region
->num
) != 0) {
194 xen_be_printf(&blkdev
->xendev
, 0,
195 "xc_gnttab_munmap region %p failed: %s\n",
196 region
->addr
, strerror(errno
));
198 xen_be_printf(&blkdev
->xendev
, 3,
199 "unmapped grant region %p with %d pages\n",
200 region
->addr
, region
->num
);
204 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
206 struct ioreq
*ioreq
= NULL
;
208 if (QLIST_EMPTY(&blkdev
->freelist
)) {
209 if (blkdev
->requests_total
>= max_requests
) {
212 /* allocate new struct */
213 ioreq
= g_malloc0(sizeof(*ioreq
));
214 ioreq
->blkdev
= blkdev
;
215 blkdev
->requests_total
++;
216 qemu_iovec_init(&ioreq
->v
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
218 /* get one from freelist */
219 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
220 QLIST_REMOVE(ioreq
, list
);
222 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
223 blkdev
->requests_inflight
++;
229 static void ioreq_finish(struct ioreq
*ioreq
)
231 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
233 QLIST_REMOVE(ioreq
, list
);
234 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
235 blkdev
->requests_inflight
--;
236 blkdev
->requests_finished
++;
239 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
241 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
243 QLIST_REMOVE(ioreq
, list
);
245 ioreq
->blkdev
= blkdev
;
246 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
248 blkdev
->requests_finished
--;
250 blkdev
->requests_inflight
--;
255 * translate request into iovec + start offset
256 * do sanity checks along the way
258 static int ioreq_parse(struct ioreq
*ioreq
)
260 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
265 xen_be_printf(&blkdev
->xendev
, 3,
266 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
267 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
268 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
269 switch (ioreq
->req
.operation
) {
271 ioreq
->prot
= PROT_WRITE
; /* to memory */
273 case BLKIF_OP_FLUSH_DISKCACHE
:
275 if (!ioreq
->req
.nr_segments
) {
280 ioreq
->prot
= PROT_READ
; /* from memory */
282 case BLKIF_OP_DISCARD
:
285 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
286 ioreq
->req
.operation
);
290 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
291 xen_be_printf(&blkdev
->xendev
, 0, "error: write req for ro device\n");
295 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
296 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
297 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
298 xen_be_printf(&blkdev
->xendev
, 0, "error: nr_segments too big\n");
301 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
302 xen_be_printf(&blkdev
->xendev
, 0, "error: first > last sector\n");
305 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
306 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
310 ioreq
->domids
[i
] = blkdev
->xendev
.dom
;
311 ioreq
->refs
[i
] = ioreq
->req
.seg
[i
].gref
;
313 mem
= ioreq
->req
.seg
[i
].first_sect
* blkdev
->file_blk
;
314 len
= (ioreq
->req
.seg
[i
].last_sect
- ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
315 qemu_iovec_add(&ioreq
->v
, (void*)mem
, len
);
317 if (ioreq
->start
+ ioreq
->v
.size
> blkdev
->file_size
) {
318 xen_be_printf(&blkdev
->xendev
, 0, "error: access beyond end of file\n");
324 ioreq
->status
= BLKIF_RSP_ERROR
;
328 static void ioreq_unmap(struct ioreq
*ioreq
)
330 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
333 if (ioreq
->num_unmap
== 0 || ioreq
->mapped
== 0) {
340 if (xc_gnttab_munmap(gnt
, ioreq
->pages
, ioreq
->num_unmap
) != 0) {
341 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
344 ioreq
->blkdev
->cnt_map
-= ioreq
->num_unmap
;
347 for (i
= 0; i
< ioreq
->num_unmap
; i
++) {
348 if (!ioreq
->page
[i
]) {
351 if (xc_gnttab_munmap(gnt
, ioreq
->page
[i
], 1) != 0) {
352 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
355 ioreq
->blkdev
->cnt_map
--;
356 ioreq
->page
[i
] = NULL
;
362 static int ioreq_map(struct ioreq
*ioreq
)
364 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
365 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
366 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
367 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
368 int i
, j
, new_maps
= 0;
369 PersistentGrant
*grant
;
370 PersistentRegion
*region
;
371 /* domids and refs variables will contain the information necessary
372 * to map the grants that are needed to fulfill this request.
374 * After mapping the needed grants, the page array will contain the
375 * memory address of each granted page in the order specified in ioreq
376 * (disregarding if it's a persistent grant or not).
379 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 1) {
382 if (ioreq
->blkdev
->feature_persistent
) {
383 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
384 grant
= g_tree_lookup(ioreq
->blkdev
->persistent_gnts
,
385 GUINT_TO_POINTER(ioreq
->refs
[i
]));
388 page
[i
] = grant
->page
;
389 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
390 "using persistent-grant %" PRIu32
"\n",
393 /* Add the grant to the list of grants that
396 domids
[new_maps
] = ioreq
->domids
[i
];
397 refs
[new_maps
] = ioreq
->refs
[i
];
402 /* Set the protection to RW, since grants may be reused later
403 * with a different protection than the one needed for this request
405 ioreq
->prot
= PROT_WRITE
| PROT_READ
;
407 /* All grants in the request should be mapped */
408 memcpy(refs
, ioreq
->refs
, sizeof(refs
));
409 memcpy(domids
, ioreq
->domids
, sizeof(domids
));
410 memset(page
, 0, sizeof(page
));
411 new_maps
= ioreq
->v
.niov
;
414 if (batch_maps
&& new_maps
) {
415 ioreq
->pages
= xc_gnttab_map_grant_refs
416 (gnt
, new_maps
, domids
, refs
, ioreq
->prot
);
417 if (ioreq
->pages
== NULL
) {
418 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
419 "can't map %d grant refs (%s, %d maps)\n",
420 new_maps
, strerror(errno
), ioreq
->blkdev
->cnt_map
);
423 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
424 if (page
[i
] == NULL
) {
425 page
[i
] = ioreq
->pages
+ (j
++) * XC_PAGE_SIZE
;
428 ioreq
->blkdev
->cnt_map
+= new_maps
;
429 } else if (new_maps
) {
430 for (i
= 0; i
< new_maps
; i
++) {
431 ioreq
->page
[i
] = xc_gnttab_map_grant_ref
432 (gnt
, domids
[i
], refs
[i
], ioreq
->prot
);
433 if (ioreq
->page
[i
] == NULL
) {
434 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
435 "can't map grant ref %d (%s, %d maps)\n",
436 refs
[i
], strerror(errno
), ioreq
->blkdev
->cnt_map
);
441 ioreq
->blkdev
->cnt_map
++;
443 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
444 if (page
[i
] == NULL
) {
445 page
[i
] = ioreq
->page
[j
++];
449 if (ioreq
->blkdev
->feature_persistent
&& new_maps
!= 0 &&
450 (!batch_maps
|| (ioreq
->blkdev
->persistent_gnt_count
+ new_maps
<=
451 ioreq
->blkdev
->max_grants
))) {
453 * If we are using persistent grants and batch mappings only
454 * add the new maps to the list of persistent grants if the whole
455 * area can be persistently mapped.
458 region
= g_malloc0(sizeof(*region
));
459 region
->addr
= ioreq
->pages
;
460 region
->num
= new_maps
;
461 ioreq
->blkdev
->persistent_regions
= g_slist_append(
462 ioreq
->blkdev
->persistent_regions
,
465 while ((ioreq
->blkdev
->persistent_gnt_count
< ioreq
->blkdev
->max_grants
)
467 /* Go through the list of newly mapped grants and add as many
468 * as possible to the list of persistently mapped grants.
470 * Since we start at the end of ioreq->page(s), we only need
471 * to decrease new_maps to prevent this granted pages from
472 * being unmapped in ioreq_unmap.
474 grant
= g_malloc0(sizeof(*grant
));
477 grant
->page
= ioreq
->pages
+ (new_maps
) * XC_PAGE_SIZE
;
479 grant
->page
= ioreq
->page
[new_maps
];
481 grant
->blkdev
= ioreq
->blkdev
;
482 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
483 "adding grant %" PRIu32
" page: %p\n",
484 refs
[new_maps
], grant
->page
);
485 g_tree_insert(ioreq
->blkdev
->persistent_gnts
,
486 GUINT_TO_POINTER(refs
[new_maps
]),
488 ioreq
->blkdev
->persistent_gnt_count
++;
490 assert(!batch_maps
|| new_maps
== 0);
492 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
493 ioreq
->v
.iov
[i
].iov_base
+= (uintptr_t)page
[i
];
496 ioreq
->num_unmap
= new_maps
;
500 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
502 static void qemu_aio_complete(void *opaque
, int ret
)
504 struct ioreq
*ioreq
= opaque
;
507 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
508 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
512 ioreq
->aio_inflight
--;
513 if (ioreq
->presync
) {
515 ioreq_runio_qemu_aio(ioreq
);
518 if (ioreq
->aio_inflight
> 0) {
522 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
525 switch (ioreq
->req
.operation
) {
527 case BLKIF_OP_FLUSH_DISKCACHE
:
528 if (!ioreq
->req
.nr_segments
) {
532 if (ioreq
->status
== BLKIF_RSP_OKAY
) {
533 block_acct_done(blk_get_stats(ioreq
->blkdev
->blk
), &ioreq
->acct
);
535 block_acct_failed(blk_get_stats(ioreq
->blkdev
->blk
), &ioreq
->acct
);
538 case BLKIF_OP_DISCARD
:
542 qemu_bh_schedule(ioreq
->blkdev
->bh
);
545 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
547 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
549 if (ioreq
->req
.nr_segments
&& ioreq_map(ioreq
) == -1) {
553 ioreq
->aio_inflight
++;
554 if (ioreq
->presync
) {
555 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
559 switch (ioreq
->req
.operation
) {
561 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
562 ioreq
->v
.size
, BLOCK_ACCT_READ
);
563 ioreq
->aio_inflight
++;
564 blk_aio_readv(blkdev
->blk
, ioreq
->start
/ BLOCK_SIZE
,
565 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
566 qemu_aio_complete
, ioreq
);
569 case BLKIF_OP_FLUSH_DISKCACHE
:
570 if (!ioreq
->req
.nr_segments
) {
574 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
576 ioreq
->req
.operation
== BLKIF_OP_WRITE
?
577 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
578 ioreq
->aio_inflight
++;
579 blk_aio_writev(blkdev
->blk
, ioreq
->start
/ BLOCK_SIZE
,
580 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
581 qemu_aio_complete
, ioreq
);
583 case BLKIF_OP_DISCARD
:
585 struct blkif_request_discard
*discard_req
= (void *)&ioreq
->req
;
586 ioreq
->aio_inflight
++;
587 blk_aio_discard(blkdev
->blk
,
588 discard_req
->sector_number
, discard_req
->nr_sectors
,
589 qemu_aio_complete
, ioreq
);
593 /* unknown operation (shouldn't happen -- parse catches this) */
597 qemu_aio_complete(ioreq
, 0);
605 ioreq
->status
= BLKIF_RSP_ERROR
;
609 static int blk_send_response_one(struct ioreq
*ioreq
)
611 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
613 int have_requests
= 0;
614 blkif_response_t resp
;
617 resp
.id
= ioreq
->req
.id
;
618 resp
.operation
= ioreq
->req
.operation
;
619 resp
.status
= ioreq
->status
;
621 /* Place on the response ring for the relevant domain. */
622 switch (blkdev
->protocol
) {
623 case BLKIF_PROTOCOL_NATIVE
:
624 dst
= RING_GET_RESPONSE(&blkdev
->rings
.native
, blkdev
->rings
.native
.rsp_prod_pvt
);
626 case BLKIF_PROTOCOL_X86_32
:
627 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_32_part
,
628 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
630 case BLKIF_PROTOCOL_X86_64
:
631 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_64_part
,
632 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
638 memcpy(dst
, &resp
, sizeof(resp
));
639 blkdev
->rings
.common
.rsp_prod_pvt
++;
641 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
642 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
644 * Tail check for pending requests. Allows frontend to avoid
645 * notifications if requests are already in flight (lower
646 * overheads and promotes batching).
648 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
649 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
659 /* walk finished list, send outstanding responses, free requests */
660 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
665 while (!QLIST_EMPTY(&blkdev
->finished
)) {
666 ioreq
= QLIST_FIRST(&blkdev
->finished
);
667 send_notify
+= blk_send_response_one(ioreq
);
668 ioreq_release(ioreq
, true);
671 xen_be_send_notify(&blkdev
->xendev
);
675 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
, RING_IDX rc
)
677 switch (blkdev
->protocol
) {
678 case BLKIF_PROTOCOL_NATIVE
:
679 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
682 case BLKIF_PROTOCOL_X86_32
:
683 blkif_get_x86_32_req(&ioreq
->req
,
684 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
686 case BLKIF_PROTOCOL_X86_64
:
687 blkif_get_x86_64_req(&ioreq
->req
,
688 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
694 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
699 blkdev
->more_work
= 0;
701 rc
= blkdev
->rings
.common
.req_cons
;
702 rp
= blkdev
->rings
.common
.sring
->req_prod
;
703 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
705 blk_send_response_all(blkdev
);
707 /* pull request from ring */
708 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
711 ioreq
= ioreq_start(blkdev
);
716 blk_get_request(blkdev
, ioreq
, rc
);
717 blkdev
->rings
.common
.req_cons
= ++rc
;
720 if (ioreq_parse(ioreq
) != 0) {
722 switch (ioreq
->req
.operation
) {
724 block_acct_invalid(blk_get_stats(blkdev
->blk
),
728 block_acct_invalid(blk_get_stats(blkdev
->blk
),
731 case BLKIF_OP_FLUSH_DISKCACHE
:
732 block_acct_invalid(blk_get_stats(blkdev
->blk
),
738 if (blk_send_response_one(ioreq
)) {
739 xen_be_send_notify(&blkdev
->xendev
);
741 ioreq_release(ioreq
, false);
745 ioreq_runio_qemu_aio(ioreq
);
748 if (blkdev
->more_work
&& blkdev
->requests_inflight
< max_requests
) {
749 qemu_bh_schedule(blkdev
->bh
);
753 /* ------------------------------------------------------------- */
755 static void blk_bh(void *opaque
)
757 struct XenBlkDev
*blkdev
= opaque
;
758 blk_handle_requests(blkdev
);
762 * We need to account for the grant allocations requiring contiguous
763 * chunks; the worst case number would be
764 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
765 * but in order to keep things simple just use
766 * 2 * max_req * max_seg.
768 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
770 static void blk_alloc(struct XenDevice
*xendev
)
772 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
774 QLIST_INIT(&blkdev
->inflight
);
775 QLIST_INIT(&blkdev
->finished
);
776 QLIST_INIT(&blkdev
->freelist
);
777 blkdev
->bh
= qemu_bh_new(blk_bh
, blkdev
);
778 if (xen_mode
!= XEN_EMULATE
) {
781 if (xc_gnttab_set_max_grants(xendev
->gnttabdev
,
782 MAX_GRANTS(max_requests
, BLKIF_MAX_SEGMENTS_PER_REQUEST
)) < 0) {
783 xen_be_printf(xendev
, 0, "xc_gnttab_set_max_grants failed: %s\n",
788 static void blk_parse_discard(struct XenBlkDev
*blkdev
)
792 blkdev
->feature_discard
= true;
794 if (xenstore_read_be_int(&blkdev
->xendev
, "discard-enable", &enable
) == 0) {
795 blkdev
->feature_discard
= !!enable
;
798 if (blkdev
->feature_discard
) {
799 xenstore_write_be_int(&blkdev
->xendev
, "feature-discard", 1);
803 static int blk_init(struct XenDevice
*xendev
)
805 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
807 char *directiosafe
= NULL
;
809 /* read xenstore entries */
810 if (blkdev
->params
== NULL
) {
812 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
813 if (blkdev
->params
!= NULL
) {
814 h
= strchr(blkdev
->params
, ':');
817 blkdev
->fileproto
= blkdev
->params
;
818 blkdev
->filename
= h
+1;
821 blkdev
->fileproto
= "<unset>";
822 blkdev
->filename
= blkdev
->params
;
825 if (!strcmp("aio", blkdev
->fileproto
)) {
826 blkdev
->fileproto
= "raw";
828 if (blkdev
->mode
== NULL
) {
829 blkdev
->mode
= xenstore_read_be_str(&blkdev
->xendev
, "mode");
831 if (blkdev
->type
== NULL
) {
832 blkdev
->type
= xenstore_read_be_str(&blkdev
->xendev
, "type");
834 if (blkdev
->dev
== NULL
) {
835 blkdev
->dev
= xenstore_read_be_str(&blkdev
->xendev
, "dev");
837 if (blkdev
->devtype
== NULL
) {
838 blkdev
->devtype
= xenstore_read_be_str(&blkdev
->xendev
, "device-type");
840 directiosafe
= xenstore_read_be_str(&blkdev
->xendev
, "direct-io-safe");
841 blkdev
->directiosafe
= (directiosafe
&& atoi(directiosafe
));
843 /* do we have all we need? */
844 if (blkdev
->params
== NULL
||
845 blkdev
->mode
== NULL
||
846 blkdev
->type
== NULL
||
847 blkdev
->dev
== NULL
) {
852 if (strcmp(blkdev
->mode
, "w")) {
853 info
|= VDISK_READONLY
;
857 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
861 blkdev
->file_blk
= BLOCK_SIZE
;
864 * blk_connect supplies sector-size and sectors
866 xenstore_write_be_int(&blkdev
->xendev
, "feature-flush-cache", 1);
867 xenstore_write_be_int(&blkdev
->xendev
, "feature-persistent", 1);
868 xenstore_write_be_int(&blkdev
->xendev
, "info", info
);
870 blk_parse_discard(blkdev
);
872 g_free(directiosafe
);
876 g_free(blkdev
->params
);
877 blkdev
->params
= NULL
;
878 g_free(blkdev
->mode
);
880 g_free(blkdev
->type
);
884 g_free(blkdev
->devtype
);
885 blkdev
->devtype
= NULL
;
886 g_free(directiosafe
);
887 blkdev
->directiosafe
= false;
891 static int blk_connect(struct XenDevice
*xendev
)
893 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
894 int pers
, index
, qflags
;
895 bool readonly
= true;
898 if (blkdev
->directiosafe
) {
899 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
901 qflags
= BDRV_O_CACHE_WB
;
903 if (strcmp(blkdev
->mode
, "w") == 0) {
904 qflags
|= BDRV_O_RDWR
;
907 if (blkdev
->feature_discard
) {
908 qflags
|= BDRV_O_UNMAP
;
911 /* init qemu block driver */
912 index
= (blkdev
->xendev
.dev
- 202 * 256) / 16;
913 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
914 if (!blkdev
->dinfo
) {
915 Error
*local_err
= NULL
;
916 QDict
*options
= NULL
;
918 if (strcmp(blkdev
->fileproto
, "<unset>")) {
919 options
= qdict_new();
920 qdict_put(options
, "driver", qstring_from_str(blkdev
->fileproto
));
923 /* setup via xenbus -> create new block driver instance */
924 xen_be_printf(&blkdev
->xendev
, 2, "create new bdrv (xenbus setup)\n");
925 blkdev
->blk
= blk_new_open(blkdev
->dev
, blkdev
->filename
, NULL
, options
,
928 xen_be_printf(&blkdev
->xendev
, 0, "error: %s\n",
929 error_get_pretty(local_err
));
930 error_free(local_err
);
934 /* setup via qemu cmdline -> already setup for us */
935 xen_be_printf(&blkdev
->xendev
, 2, "get configured bdrv (cmdline setup)\n");
936 blkdev
->blk
= blk_by_legacy_dinfo(blkdev
->dinfo
);
937 if (blk_is_read_only(blkdev
->blk
) && !readonly
) {
938 xen_be_printf(&blkdev
->xendev
, 0, "Unexpected read-only drive");
942 /* blkdev->blk is not create by us, we get a reference
943 * so we can blk_unref() unconditionally */
944 blk_ref(blkdev
->blk
);
946 blk_attach_dev_nofail(blkdev
->blk
, blkdev
);
947 blkdev
->file_size
= blk_getlength(blkdev
->blk
);
948 if (blkdev
->file_size
< 0) {
949 BlockDriverState
*bs
= blk_bs(blkdev
->blk
);
950 const char *drv_name
= bs
? bdrv_get_format_name(bs
) : NULL
;
951 xen_be_printf(&blkdev
->xendev
, 1, "blk_getlength: %d (%s) | drv %s\n",
952 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
954 blkdev
->file_size
= 0;
957 xen_be_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
958 " size %" PRId64
" (%" PRId64
" MB)\n",
959 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
960 blkdev
->file_size
, blkdev
->file_size
>> 20);
962 /* Fill in number of sector size and number of sectors */
963 xenstore_write_be_int(&blkdev
->xendev
, "sector-size", blkdev
->file_blk
);
964 xenstore_write_be_int64(&blkdev
->xendev
, "sectors",
965 blkdev
->file_size
/ blkdev
->file_blk
);
967 if (xenstore_read_fe_int(&blkdev
->xendev
, "ring-ref", &blkdev
->ring_ref
) == -1) {
970 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
971 &blkdev
->xendev
.remote_port
) == -1) {
974 if (xenstore_read_fe_int(&blkdev
->xendev
, "feature-persistent", &pers
)) {
975 blkdev
->feature_persistent
= FALSE
;
977 blkdev
->feature_persistent
= !!pers
;
980 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
981 if (blkdev
->xendev
.protocol
) {
982 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
983 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
985 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
986 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
990 blkdev
->sring
= xc_gnttab_map_grant_ref(blkdev
->xendev
.gnttabdev
,
993 PROT_READ
| PROT_WRITE
);
994 if (!blkdev
->sring
) {
999 switch (blkdev
->protocol
) {
1000 case BLKIF_PROTOCOL_NATIVE
:
1002 blkif_sring_t
*sring_native
= blkdev
->sring
;
1003 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, XC_PAGE_SIZE
);
1006 case BLKIF_PROTOCOL_X86_32
:
1008 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
1010 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, XC_PAGE_SIZE
);
1013 case BLKIF_PROTOCOL_X86_64
:
1015 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
1017 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, XC_PAGE_SIZE
);
1022 if (blkdev
->feature_persistent
) {
1023 /* Init persistent grants */
1024 blkdev
->max_grants
= max_requests
* BLKIF_MAX_SEGMENTS_PER_REQUEST
;
1025 blkdev
->persistent_gnts
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
1028 (GDestroyNotify
)g_free
:
1029 (GDestroyNotify
)destroy_grant
);
1030 blkdev
->persistent_regions
= NULL
;
1031 blkdev
->persistent_gnt_count
= 0;
1034 xen_be_bind_evtchn(&blkdev
->xendev
);
1036 xen_be_printf(&blkdev
->xendev
, 1, "ok: proto %s, ring-ref %d, "
1037 "remote port %d, local port %d\n",
1038 blkdev
->xendev
.protocol
, blkdev
->ring_ref
,
1039 blkdev
->xendev
.remote_port
, blkdev
->xendev
.local_port
);
1043 static void blk_disconnect(struct XenDevice
*xendev
)
1045 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1048 blk_detach_dev(blkdev
->blk
, blkdev
);
1049 blk_unref(blkdev
->blk
);
1052 xen_be_unbind_evtchn(&blkdev
->xendev
);
1054 if (blkdev
->sring
) {
1055 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
1057 blkdev
->sring
= NULL
;
1061 * Unmap persistent grants before switching to the closed state
1062 * so the frontend can free them.
1064 * In the !batch_maps case g_tree_destroy will take care of unmapping
1065 * the grant, but in the batch_maps case we need to iterate over every
1066 * region in persistent_regions and unmap it.
1068 if (blkdev
->feature_persistent
) {
1069 g_tree_destroy(blkdev
->persistent_gnts
);
1070 assert(batch_maps
|| blkdev
->persistent_gnt_count
== 0);
1072 blkdev
->persistent_gnt_count
= 0;
1073 g_slist_foreach(blkdev
->persistent_regions
,
1074 (GFunc
)remove_persistent_region
, blkdev
);
1075 g_slist_free(blkdev
->persistent_regions
);
1077 blkdev
->feature_persistent
= false;
1081 static int blk_free(struct XenDevice
*xendev
)
1083 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1084 struct ioreq
*ioreq
;
1086 if (blkdev
->blk
|| blkdev
->sring
) {
1087 blk_disconnect(xendev
);
1090 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
1091 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
1092 QLIST_REMOVE(ioreq
, list
);
1093 qemu_iovec_destroy(&ioreq
->v
);
1097 g_free(blkdev
->params
);
1098 g_free(blkdev
->mode
);
1099 g_free(blkdev
->type
);
1100 g_free(blkdev
->dev
);
1101 g_free(blkdev
->devtype
);
1102 qemu_bh_delete(blkdev
->bh
);
1106 static void blk_event(struct XenDevice
*xendev
)
1108 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1110 qemu_bh_schedule(blkdev
->bh
);
1113 struct XenDevOps xen_blkdev_ops
= {
1114 .size
= sizeof(struct XenBlkDev
),
1115 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
1118 .initialise
= blk_connect
,
1119 .disconnect
= blk_disconnect
,