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"
43 /* ------------------------------------------------------------- */
45 static int batch_maps
= 0;
47 static int max_requests
= 32;
49 /* ------------------------------------------------------------- */
51 #define BLOCK_SIZE 512
52 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
54 struct PersistentGrant
{
56 struct XenBlkDev
*blkdev
;
59 typedef struct PersistentGrant PersistentGrant
;
73 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
74 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
76 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
84 struct XenBlkDev
*blkdev
;
85 QLIST_ENTRY(ioreq
) list
;
90 struct XenDevice xendev
; /* must be first */
97 const char *fileproto
;
104 blkif_back_rings_t rings
;
109 QLIST_HEAD(inflight_head
, ioreq
) inflight
;
110 QLIST_HEAD(finished_head
, ioreq
) finished
;
111 QLIST_HEAD(freelist_head
, ioreq
) freelist
;
113 int requests_inflight
;
114 int requests_finished
;
116 /* Persistent grants extension */
117 gboolean feature_persistent
;
118 GTree
*persistent_gnts
;
119 unsigned int persistent_gnt_count
;
120 unsigned int max_grants
;
122 /* qemu block driver */
124 BlockDriverState
*bs
;
128 /* ------------------------------------------------------------- */
130 static void ioreq_reset(struct ioreq
*ioreq
)
132 memset(&ioreq
->req
, 0, sizeof(ioreq
->req
));
139 memset(ioreq
->domids
, 0, sizeof(ioreq
->domids
));
140 memset(ioreq
->refs
, 0, sizeof(ioreq
->refs
));
142 memset(ioreq
->page
, 0, sizeof(ioreq
->page
));
145 ioreq
->aio_inflight
= 0;
146 ioreq
->aio_errors
= 0;
148 ioreq
->blkdev
= NULL
;
149 memset(&ioreq
->list
, 0, sizeof(ioreq
->list
));
150 memset(&ioreq
->acct
, 0, sizeof(ioreq
->acct
));
152 qemu_iovec_reset(&ioreq
->v
);
155 static gint
int_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
157 uint ua
= GPOINTER_TO_UINT(a
);
158 uint ub
= GPOINTER_TO_UINT(b
);
159 return (ua
> ub
) - (ua
< ub
);
162 static void destroy_grant(gpointer pgnt
)
164 PersistentGrant
*grant
= pgnt
;
165 XenGnttab gnt
= grant
->blkdev
->xendev
.gnttabdev
;
167 if (xc_gnttab_munmap(gnt
, grant
->page
, 1) != 0) {
168 xen_be_printf(&grant
->blkdev
->xendev
, 0,
169 "xc_gnttab_munmap failed: %s\n",
172 grant
->blkdev
->persistent_gnt_count
--;
173 xen_be_printf(&grant
->blkdev
->xendev
, 3,
174 "unmapped grant %p\n", grant
->page
);
178 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
180 struct ioreq
*ioreq
= NULL
;
182 if (QLIST_EMPTY(&blkdev
->freelist
)) {
183 if (blkdev
->requests_total
>= max_requests
) {
186 /* allocate new struct */
187 ioreq
= g_malloc0(sizeof(*ioreq
));
188 ioreq
->blkdev
= blkdev
;
189 blkdev
->requests_total
++;
190 qemu_iovec_init(&ioreq
->v
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
192 /* get one from freelist */
193 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
194 QLIST_REMOVE(ioreq
, list
);
196 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
197 blkdev
->requests_inflight
++;
203 static void ioreq_finish(struct ioreq
*ioreq
)
205 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
207 QLIST_REMOVE(ioreq
, list
);
208 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
209 blkdev
->requests_inflight
--;
210 blkdev
->requests_finished
++;
213 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
215 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
217 QLIST_REMOVE(ioreq
, list
);
219 ioreq
->blkdev
= blkdev
;
220 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
222 blkdev
->requests_finished
--;
224 blkdev
->requests_inflight
--;
229 * translate request into iovec + start offset
230 * do sanity checks along the way
232 static int ioreq_parse(struct ioreq
*ioreq
)
234 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
239 xen_be_printf(&blkdev
->xendev
, 3,
240 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
241 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
242 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
243 switch (ioreq
->req
.operation
) {
245 ioreq
->prot
= PROT_WRITE
; /* to memory */
247 case BLKIF_OP_FLUSH_DISKCACHE
:
249 if (!ioreq
->req
.nr_segments
) {
254 ioreq
->prot
= PROT_READ
; /* from memory */
257 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
258 ioreq
->req
.operation
);
262 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
263 xen_be_printf(&blkdev
->xendev
, 0, "error: write req for ro device\n");
267 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
268 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
269 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
270 xen_be_printf(&blkdev
->xendev
, 0, "error: nr_segments too big\n");
273 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
274 xen_be_printf(&blkdev
->xendev
, 0, "error: first > last sector\n");
277 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
278 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
282 ioreq
->domids
[i
] = blkdev
->xendev
.dom
;
283 ioreq
->refs
[i
] = ioreq
->req
.seg
[i
].gref
;
285 mem
= ioreq
->req
.seg
[i
].first_sect
* blkdev
->file_blk
;
286 len
= (ioreq
->req
.seg
[i
].last_sect
- ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
287 qemu_iovec_add(&ioreq
->v
, (void*)mem
, len
);
289 if (ioreq
->start
+ ioreq
->v
.size
> blkdev
->file_size
) {
290 xen_be_printf(&blkdev
->xendev
, 0, "error: access beyond end of file\n");
296 ioreq
->status
= BLKIF_RSP_ERROR
;
300 static void ioreq_unmap(struct ioreq
*ioreq
)
302 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
305 if (ioreq
->num_unmap
== 0 || ioreq
->mapped
== 0) {
312 if (xc_gnttab_munmap(gnt
, ioreq
->pages
, ioreq
->num_unmap
) != 0) {
313 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
316 ioreq
->blkdev
->cnt_map
-= ioreq
->num_unmap
;
319 for (i
= 0; i
< ioreq
->num_unmap
; i
++) {
320 if (!ioreq
->page
[i
]) {
323 if (xc_gnttab_munmap(gnt
, ioreq
->page
[i
], 1) != 0) {
324 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
327 ioreq
->blkdev
->cnt_map
--;
328 ioreq
->page
[i
] = NULL
;
334 static int ioreq_map(struct ioreq
*ioreq
)
336 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
337 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
338 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
339 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
340 int i
, j
, new_maps
= 0;
341 PersistentGrant
*grant
;
342 /* domids and refs variables will contain the information necessary
343 * to map the grants that are needed to fulfill this request.
345 * After mapping the needed grants, the page array will contain the
346 * memory address of each granted page in the order specified in ioreq
347 * (disregarding if it's a persistent grant or not).
350 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 1) {
353 if (ioreq
->blkdev
->feature_persistent
) {
354 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
355 grant
= g_tree_lookup(ioreq
->blkdev
->persistent_gnts
,
356 GUINT_TO_POINTER(ioreq
->refs
[i
]));
359 page
[i
] = grant
->page
;
360 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
361 "using persistent-grant %" PRIu32
"\n",
364 /* Add the grant to the list of grants that
367 domids
[new_maps
] = ioreq
->domids
[i
];
368 refs
[new_maps
] = ioreq
->refs
[i
];
373 /* Set the protection to RW, since grants may be reused later
374 * with a different protection than the one needed for this request
376 ioreq
->prot
= PROT_WRITE
| PROT_READ
;
378 /* All grants in the request should be mapped */
379 memcpy(refs
, ioreq
->refs
, sizeof(refs
));
380 memcpy(domids
, ioreq
->domids
, sizeof(domids
));
381 memset(page
, 0, sizeof(page
));
382 new_maps
= ioreq
->v
.niov
;
385 if (batch_maps
&& new_maps
) {
386 ioreq
->pages
= xc_gnttab_map_grant_refs
387 (gnt
, new_maps
, domids
, refs
, ioreq
->prot
);
388 if (ioreq
->pages
== NULL
) {
389 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
390 "can't map %d grant refs (%s, %d maps)\n",
391 new_maps
, strerror(errno
), ioreq
->blkdev
->cnt_map
);
394 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
395 if (page
[i
] == NULL
) {
396 page
[i
] = ioreq
->pages
+ (j
++) * XC_PAGE_SIZE
;
399 ioreq
->blkdev
->cnt_map
+= new_maps
;
400 } else if (new_maps
) {
401 for (i
= 0; i
< new_maps
; i
++) {
402 ioreq
->page
[i
] = xc_gnttab_map_grant_ref
403 (gnt
, domids
[i
], refs
[i
], ioreq
->prot
);
404 if (ioreq
->page
[i
] == NULL
) {
405 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
406 "can't map grant ref %d (%s, %d maps)\n",
407 refs
[i
], strerror(errno
), ioreq
->blkdev
->cnt_map
);
411 ioreq
->blkdev
->cnt_map
++;
413 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
414 if (page
[i
] == NULL
) {
415 page
[i
] = ioreq
->page
[j
++];
419 if (ioreq
->blkdev
->feature_persistent
) {
420 while ((ioreq
->blkdev
->persistent_gnt_count
< ioreq
->blkdev
->max_grants
)
422 /* Go through the list of newly mapped grants and add as many
423 * as possible to the list of persistently mapped grants.
425 * Since we start at the end of ioreq->page(s), we only need
426 * to decrease new_maps to prevent this granted pages from
427 * being unmapped in ioreq_unmap.
429 grant
= g_malloc0(sizeof(*grant
));
432 grant
->page
= ioreq
->pages
+ (new_maps
) * XC_PAGE_SIZE
;
434 grant
->page
= ioreq
->page
[new_maps
];
436 grant
->blkdev
= ioreq
->blkdev
;
437 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
438 "adding grant %" PRIu32
" page: %p\n",
439 refs
[new_maps
], grant
->page
);
440 g_tree_insert(ioreq
->blkdev
->persistent_gnts
,
441 GUINT_TO_POINTER(refs
[new_maps
]),
443 ioreq
->blkdev
->persistent_gnt_count
++;
446 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
447 ioreq
->v
.iov
[i
].iov_base
+= (uintptr_t)page
[i
];
450 ioreq
->num_unmap
= new_maps
;
454 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
456 static void qemu_aio_complete(void *opaque
, int ret
)
458 struct ioreq
*ioreq
= opaque
;
461 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
462 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
466 ioreq
->aio_inflight
--;
467 if (ioreq
->presync
) {
469 ioreq_runio_qemu_aio(ioreq
);
472 if (ioreq
->aio_inflight
> 0) {
475 if (ioreq
->postsync
) {
477 ioreq
->aio_inflight
++;
478 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
482 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
485 bdrv_acct_done(ioreq
->blkdev
->bs
, &ioreq
->acct
);
486 qemu_bh_schedule(ioreq
->blkdev
->bh
);
489 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
491 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
493 if (ioreq
->req
.nr_segments
&& ioreq_map(ioreq
) == -1) {
497 ioreq
->aio_inflight
++;
498 if (ioreq
->presync
) {
499 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
503 switch (ioreq
->req
.operation
) {
505 bdrv_acct_start(blkdev
->bs
, &ioreq
->acct
, ioreq
->v
.size
, BDRV_ACCT_READ
);
506 ioreq
->aio_inflight
++;
507 bdrv_aio_readv(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
508 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
509 qemu_aio_complete
, ioreq
);
512 case BLKIF_OP_FLUSH_DISKCACHE
:
513 if (!ioreq
->req
.nr_segments
) {
517 bdrv_acct_start(blkdev
->bs
, &ioreq
->acct
, ioreq
->v
.size
, BDRV_ACCT_WRITE
);
518 ioreq
->aio_inflight
++;
519 bdrv_aio_writev(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
520 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
521 qemu_aio_complete
, ioreq
);
524 /* unknown operation (shouldn't happen -- parse catches this) */
528 qemu_aio_complete(ioreq
, 0);
536 ioreq
->status
= BLKIF_RSP_ERROR
;
540 static int blk_send_response_one(struct ioreq
*ioreq
)
542 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
544 int have_requests
= 0;
545 blkif_response_t resp
;
548 resp
.id
= ioreq
->req
.id
;
549 resp
.operation
= ioreq
->req
.operation
;
550 resp
.status
= ioreq
->status
;
552 /* Place on the response ring for the relevant domain. */
553 switch (blkdev
->protocol
) {
554 case BLKIF_PROTOCOL_NATIVE
:
555 dst
= RING_GET_RESPONSE(&blkdev
->rings
.native
, blkdev
->rings
.native
.rsp_prod_pvt
);
557 case BLKIF_PROTOCOL_X86_32
:
558 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_32_part
,
559 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
561 case BLKIF_PROTOCOL_X86_64
:
562 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_64_part
,
563 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
568 memcpy(dst
, &resp
, sizeof(resp
));
569 blkdev
->rings
.common
.rsp_prod_pvt
++;
571 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
572 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
574 * Tail check for pending requests. Allows frontend to avoid
575 * notifications if requests are already in flight (lower
576 * overheads and promotes batching).
578 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
579 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
589 /* walk finished list, send outstanding responses, free requests */
590 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
595 while (!QLIST_EMPTY(&blkdev
->finished
)) {
596 ioreq
= QLIST_FIRST(&blkdev
->finished
);
597 send_notify
+= blk_send_response_one(ioreq
);
598 ioreq_release(ioreq
, true);
601 xen_be_send_notify(&blkdev
->xendev
);
605 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
, RING_IDX rc
)
607 switch (blkdev
->protocol
) {
608 case BLKIF_PROTOCOL_NATIVE
:
609 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
612 case BLKIF_PROTOCOL_X86_32
:
613 blkif_get_x86_32_req(&ioreq
->req
,
614 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
616 case BLKIF_PROTOCOL_X86_64
:
617 blkif_get_x86_64_req(&ioreq
->req
,
618 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
624 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
629 blkdev
->more_work
= 0;
631 rc
= blkdev
->rings
.common
.req_cons
;
632 rp
= blkdev
->rings
.common
.sring
->req_prod
;
633 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
635 blk_send_response_all(blkdev
);
637 /* pull request from ring */
638 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
641 ioreq
= ioreq_start(blkdev
);
646 blk_get_request(blkdev
, ioreq
, rc
);
647 blkdev
->rings
.common
.req_cons
= ++rc
;
650 if (ioreq_parse(ioreq
) != 0) {
651 if (blk_send_response_one(ioreq
)) {
652 xen_be_send_notify(&blkdev
->xendev
);
654 ioreq_release(ioreq
, false);
658 ioreq_runio_qemu_aio(ioreq
);
661 if (blkdev
->more_work
&& blkdev
->requests_inflight
< max_requests
) {
662 qemu_bh_schedule(blkdev
->bh
);
666 /* ------------------------------------------------------------- */
668 static void blk_bh(void *opaque
)
670 struct XenBlkDev
*blkdev
= opaque
;
671 blk_handle_requests(blkdev
);
675 * We need to account for the grant allocations requiring contiguous
676 * chunks; the worst case number would be
677 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
678 * but in order to keep things simple just use
679 * 2 * max_req * max_seg.
681 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
683 static void blk_alloc(struct XenDevice
*xendev
)
685 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
687 QLIST_INIT(&blkdev
->inflight
);
688 QLIST_INIT(&blkdev
->finished
);
689 QLIST_INIT(&blkdev
->freelist
);
690 blkdev
->bh
= qemu_bh_new(blk_bh
, blkdev
);
691 if (xen_mode
!= XEN_EMULATE
) {
694 if (xc_gnttab_set_max_grants(xendev
->gnttabdev
,
695 MAX_GRANTS(max_requests
, BLKIF_MAX_SEGMENTS_PER_REQUEST
)) < 0) {
696 xen_be_printf(xendev
, 0, "xc_gnttab_set_max_grants failed: %s\n",
701 static int blk_init(struct XenDevice
*xendev
)
703 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
705 char *directiosafe
= NULL
;
707 /* read xenstore entries */
708 if (blkdev
->params
== NULL
) {
710 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
711 if (blkdev
->params
!= NULL
) {
712 h
= strchr(blkdev
->params
, ':');
715 blkdev
->fileproto
= blkdev
->params
;
716 blkdev
->filename
= h
+1;
719 blkdev
->fileproto
= "<unset>";
720 blkdev
->filename
= blkdev
->params
;
723 if (!strcmp("aio", blkdev
->fileproto
)) {
724 blkdev
->fileproto
= "raw";
726 if (blkdev
->mode
== NULL
) {
727 blkdev
->mode
= xenstore_read_be_str(&blkdev
->xendev
, "mode");
729 if (blkdev
->type
== NULL
) {
730 blkdev
->type
= xenstore_read_be_str(&blkdev
->xendev
, "type");
732 if (blkdev
->dev
== NULL
) {
733 blkdev
->dev
= xenstore_read_be_str(&blkdev
->xendev
, "dev");
735 if (blkdev
->devtype
== NULL
) {
736 blkdev
->devtype
= xenstore_read_be_str(&blkdev
->xendev
, "device-type");
738 directiosafe
= xenstore_read_be_str(&blkdev
->xendev
, "direct-io-safe");
739 blkdev
->directiosafe
= (directiosafe
&& atoi(directiosafe
));
741 /* do we have all we need? */
742 if (blkdev
->params
== NULL
||
743 blkdev
->mode
== NULL
||
744 blkdev
->type
== NULL
||
745 blkdev
->dev
== NULL
) {
750 if (strcmp(blkdev
->mode
, "w")) {
751 info
|= VDISK_READONLY
;
755 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
759 blkdev
->file_blk
= BLOCK_SIZE
;
762 * blk_connect supplies sector-size and sectors
764 xenstore_write_be_int(&blkdev
->xendev
, "feature-flush-cache", 1);
765 xenstore_write_be_int(&blkdev
->xendev
, "feature-persistent", 1);
766 xenstore_write_be_int(&blkdev
->xendev
, "info", info
);
768 g_free(directiosafe
);
772 g_free(blkdev
->params
);
773 blkdev
->params
= NULL
;
774 g_free(blkdev
->mode
);
776 g_free(blkdev
->type
);
780 g_free(blkdev
->devtype
);
781 blkdev
->devtype
= NULL
;
782 g_free(directiosafe
);
783 blkdev
->directiosafe
= false;
787 static int blk_connect(struct XenDevice
*xendev
)
789 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
790 int pers
, index
, qflags
;
791 bool readonly
= true;
794 if (blkdev
->directiosafe
) {
795 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
797 qflags
= BDRV_O_CACHE_WB
;
799 if (strcmp(blkdev
->mode
, "w") == 0) {
800 qflags
|= BDRV_O_RDWR
;
804 /* init qemu block driver */
805 index
= (blkdev
->xendev
.dev
- 202 * 256) / 16;
806 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
807 if (!blkdev
->dinfo
) {
808 /* setup via xenbus -> create new block driver instance */
809 xen_be_printf(&blkdev
->xendev
, 2, "create new bdrv (xenbus setup)\n");
810 blkdev
->bs
= bdrv_new(blkdev
->dev
);
812 BlockDriver
*drv
= bdrv_find_whitelisted_format(blkdev
->fileproto
,
814 if (bdrv_open(blkdev
->bs
,
815 blkdev
->filename
, NULL
, qflags
, drv
) != 0) {
816 bdrv_delete(blkdev
->bs
);
824 /* setup via qemu cmdline -> already setup for us */
825 xen_be_printf(&blkdev
->xendev
, 2, "get configured bdrv (cmdline setup)\n");
826 blkdev
->bs
= blkdev
->dinfo
->bdrv
;
828 bdrv_attach_dev_nofail(blkdev
->bs
, blkdev
);
829 blkdev
->file_size
= bdrv_getlength(blkdev
->bs
);
830 if (blkdev
->file_size
< 0) {
831 xen_be_printf(&blkdev
->xendev
, 1, "bdrv_getlength: %d (%s) | drv %s\n",
832 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
833 bdrv_get_format_name(blkdev
->bs
) ?: "-");
834 blkdev
->file_size
= 0;
837 xen_be_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
838 " size %" PRId64
" (%" PRId64
" MB)\n",
839 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
840 blkdev
->file_size
, blkdev
->file_size
>> 20);
842 /* Fill in number of sector size and number of sectors */
843 xenstore_write_be_int(&blkdev
->xendev
, "sector-size", blkdev
->file_blk
);
844 xenstore_write_be_int64(&blkdev
->xendev
, "sectors",
845 blkdev
->file_size
/ blkdev
->file_blk
);
847 if (xenstore_read_fe_int(&blkdev
->xendev
, "ring-ref", &blkdev
->ring_ref
) == -1) {
850 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
851 &blkdev
->xendev
.remote_port
) == -1) {
854 if (xenstore_read_fe_int(&blkdev
->xendev
, "feature-persistent", &pers
)) {
855 blkdev
->feature_persistent
= FALSE
;
857 blkdev
->feature_persistent
= !!pers
;
860 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
861 if (blkdev
->xendev
.protocol
) {
862 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
863 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
865 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
866 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
870 blkdev
->sring
= xc_gnttab_map_grant_ref(blkdev
->xendev
.gnttabdev
,
873 PROT_READ
| PROT_WRITE
);
874 if (!blkdev
->sring
) {
879 switch (blkdev
->protocol
) {
880 case BLKIF_PROTOCOL_NATIVE
:
882 blkif_sring_t
*sring_native
= blkdev
->sring
;
883 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, XC_PAGE_SIZE
);
886 case BLKIF_PROTOCOL_X86_32
:
888 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
890 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, XC_PAGE_SIZE
);
893 case BLKIF_PROTOCOL_X86_64
:
895 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
897 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, XC_PAGE_SIZE
);
902 if (blkdev
->feature_persistent
) {
903 /* Init persistent grants */
904 blkdev
->max_grants
= max_requests
* BLKIF_MAX_SEGMENTS_PER_REQUEST
;
905 blkdev
->persistent_gnts
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
907 (GDestroyNotify
)destroy_grant
);
908 blkdev
->persistent_gnt_count
= 0;
911 xen_be_bind_evtchn(&blkdev
->xendev
);
913 xen_be_printf(&blkdev
->xendev
, 1, "ok: proto %s, ring-ref %d, "
914 "remote port %d, local port %d\n",
915 blkdev
->xendev
.protocol
, blkdev
->ring_ref
,
916 blkdev
->xendev
.remote_port
, blkdev
->xendev
.local_port
);
920 static void blk_disconnect(struct XenDevice
*xendev
)
922 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
925 if (!blkdev
->dinfo
) {
926 /* close/delete only if we created it ourself */
927 bdrv_close(blkdev
->bs
);
928 bdrv_detach_dev(blkdev
->bs
, blkdev
);
929 bdrv_delete(blkdev
->bs
);
933 xen_be_unbind_evtchn(&blkdev
->xendev
);
936 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
938 blkdev
->sring
= NULL
;
942 static int blk_free(struct XenDevice
*xendev
)
944 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
947 if (blkdev
->bs
|| blkdev
->sring
) {
948 blk_disconnect(xendev
);
951 /* Free persistent grants */
952 if (blkdev
->feature_persistent
) {
953 g_tree_destroy(blkdev
->persistent_gnts
);
956 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
957 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
958 QLIST_REMOVE(ioreq
, list
);
959 qemu_iovec_destroy(&ioreq
->v
);
963 g_free(blkdev
->params
);
964 g_free(blkdev
->mode
);
965 g_free(blkdev
->type
);
967 g_free(blkdev
->devtype
);
968 qemu_bh_delete(blkdev
->bh
);
972 static void blk_event(struct XenDevice
*xendev
)
974 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
976 qemu_bh_schedule(blkdev
->bh
);
979 struct XenDevOps xen_blkdev_ops
= {
980 .size
= sizeof(struct XenBlkDev
),
981 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
984 .initialise
= blk_connect
,
985 .disconnect
= blk_disconnect
,