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
;
74 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
75 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
77 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
85 struct XenBlkDev
*blkdev
;
86 QLIST_ENTRY(ioreq
) list
;
91 struct XenDevice xendev
; /* must be first */
98 const char *fileproto
;
105 blkif_back_rings_t rings
;
110 QLIST_HEAD(inflight_head
, ioreq
) inflight
;
111 QLIST_HEAD(finished_head
, ioreq
) finished
;
112 QLIST_HEAD(freelist_head
, ioreq
) freelist
;
114 int requests_inflight
;
115 int requests_finished
;
117 /* Persistent grants extension */
118 gboolean feature_discard
;
119 gboolean feature_persistent
;
120 GTree
*persistent_gnts
;
121 unsigned int persistent_gnt_count
;
122 unsigned int max_grants
;
124 /* qemu block driver */
130 /* ------------------------------------------------------------- */
132 static void ioreq_reset(struct ioreq
*ioreq
)
134 memset(&ioreq
->req
, 0, sizeof(ioreq
->req
));
141 memset(ioreq
->domids
, 0, sizeof(ioreq
->domids
));
142 memset(ioreq
->refs
, 0, sizeof(ioreq
->refs
));
144 memset(ioreq
->page
, 0, sizeof(ioreq
->page
));
147 ioreq
->aio_inflight
= 0;
148 ioreq
->aio_errors
= 0;
150 ioreq
->blkdev
= NULL
;
151 memset(&ioreq
->list
, 0, sizeof(ioreq
->list
));
152 memset(&ioreq
->acct
, 0, sizeof(ioreq
->acct
));
154 qemu_iovec_reset(&ioreq
->v
);
157 static gint
int_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
159 uint ua
= GPOINTER_TO_UINT(a
);
160 uint ub
= GPOINTER_TO_UINT(b
);
161 return (ua
> ub
) - (ua
< ub
);
164 static void destroy_grant(gpointer pgnt
)
166 PersistentGrant
*grant
= pgnt
;
167 XenGnttab gnt
= grant
->blkdev
->xendev
.gnttabdev
;
169 if (xc_gnttab_munmap(gnt
, grant
->page
, 1) != 0) {
170 xen_be_printf(&grant
->blkdev
->xendev
, 0,
171 "xc_gnttab_munmap failed: %s\n",
174 grant
->blkdev
->persistent_gnt_count
--;
175 xen_be_printf(&grant
->blkdev
->xendev
, 3,
176 "unmapped grant %p\n", grant
->page
);
180 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
182 struct ioreq
*ioreq
= NULL
;
184 if (QLIST_EMPTY(&blkdev
->freelist
)) {
185 if (blkdev
->requests_total
>= max_requests
) {
188 /* allocate new struct */
189 ioreq
= g_malloc0(sizeof(*ioreq
));
190 ioreq
->blkdev
= blkdev
;
191 blkdev
->requests_total
++;
192 qemu_iovec_init(&ioreq
->v
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
194 /* get one from freelist */
195 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
196 QLIST_REMOVE(ioreq
, list
);
198 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
199 blkdev
->requests_inflight
++;
205 static void ioreq_finish(struct ioreq
*ioreq
)
207 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
209 QLIST_REMOVE(ioreq
, list
);
210 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
211 blkdev
->requests_inflight
--;
212 blkdev
->requests_finished
++;
215 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
217 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
219 QLIST_REMOVE(ioreq
, list
);
221 ioreq
->blkdev
= blkdev
;
222 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
224 blkdev
->requests_finished
--;
226 blkdev
->requests_inflight
--;
231 * translate request into iovec + start offset
232 * do sanity checks along the way
234 static int ioreq_parse(struct ioreq
*ioreq
)
236 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
241 xen_be_printf(&blkdev
->xendev
, 3,
242 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
243 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
244 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
245 switch (ioreq
->req
.operation
) {
247 ioreq
->prot
= PROT_WRITE
; /* to memory */
249 case BLKIF_OP_FLUSH_DISKCACHE
:
251 if (!ioreq
->req
.nr_segments
) {
256 ioreq
->prot
= PROT_READ
; /* from memory */
258 case BLKIF_OP_DISCARD
:
261 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
262 ioreq
->req
.operation
);
266 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
267 xen_be_printf(&blkdev
->xendev
, 0, "error: write req for ro device\n");
271 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
272 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
273 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
274 xen_be_printf(&blkdev
->xendev
, 0, "error: nr_segments too big\n");
277 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
278 xen_be_printf(&blkdev
->xendev
, 0, "error: first > last sector\n");
281 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
282 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
286 ioreq
->domids
[i
] = blkdev
->xendev
.dom
;
287 ioreq
->refs
[i
] = ioreq
->req
.seg
[i
].gref
;
289 mem
= ioreq
->req
.seg
[i
].first_sect
* blkdev
->file_blk
;
290 len
= (ioreq
->req
.seg
[i
].last_sect
- ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
291 qemu_iovec_add(&ioreq
->v
, (void*)mem
, len
);
293 if (ioreq
->start
+ ioreq
->v
.size
> blkdev
->file_size
) {
294 xen_be_printf(&blkdev
->xendev
, 0, "error: access beyond end of file\n");
300 ioreq
->status
= BLKIF_RSP_ERROR
;
304 static void ioreq_unmap(struct ioreq
*ioreq
)
306 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
309 if (ioreq
->num_unmap
== 0 || ioreq
->mapped
== 0) {
316 if (xc_gnttab_munmap(gnt
, ioreq
->pages
, ioreq
->num_unmap
) != 0) {
317 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
320 ioreq
->blkdev
->cnt_map
-= ioreq
->num_unmap
;
323 for (i
= 0; i
< ioreq
->num_unmap
; i
++) {
324 if (!ioreq
->page
[i
]) {
327 if (xc_gnttab_munmap(gnt
, ioreq
->page
[i
], 1) != 0) {
328 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
331 ioreq
->blkdev
->cnt_map
--;
332 ioreq
->page
[i
] = NULL
;
338 static int ioreq_map(struct ioreq
*ioreq
)
340 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
341 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
342 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
343 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
344 int i
, j
, new_maps
= 0;
345 PersistentGrant
*grant
;
346 /* domids and refs variables will contain the information necessary
347 * to map the grants that are needed to fulfill this request.
349 * After mapping the needed grants, the page array will contain the
350 * memory address of each granted page in the order specified in ioreq
351 * (disregarding if it's a persistent grant or not).
354 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 1) {
357 if (ioreq
->blkdev
->feature_persistent
) {
358 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
359 grant
= g_tree_lookup(ioreq
->blkdev
->persistent_gnts
,
360 GUINT_TO_POINTER(ioreq
->refs
[i
]));
363 page
[i
] = grant
->page
;
364 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
365 "using persistent-grant %" PRIu32
"\n",
368 /* Add the grant to the list of grants that
371 domids
[new_maps
] = ioreq
->domids
[i
];
372 refs
[new_maps
] = ioreq
->refs
[i
];
377 /* Set the protection to RW, since grants may be reused later
378 * with a different protection than the one needed for this request
380 ioreq
->prot
= PROT_WRITE
| PROT_READ
;
382 /* All grants in the request should be mapped */
383 memcpy(refs
, ioreq
->refs
, sizeof(refs
));
384 memcpy(domids
, ioreq
->domids
, sizeof(domids
));
385 memset(page
, 0, sizeof(page
));
386 new_maps
= ioreq
->v
.niov
;
389 if (batch_maps
&& new_maps
) {
390 ioreq
->pages
= xc_gnttab_map_grant_refs
391 (gnt
, new_maps
, domids
, refs
, ioreq
->prot
);
392 if (ioreq
->pages
== NULL
) {
393 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
394 "can't map %d grant refs (%s, %d maps)\n",
395 new_maps
, strerror(errno
), ioreq
->blkdev
->cnt_map
);
398 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
399 if (page
[i
] == NULL
) {
400 page
[i
] = ioreq
->pages
+ (j
++) * XC_PAGE_SIZE
;
403 ioreq
->blkdev
->cnt_map
+= new_maps
;
404 } else if (new_maps
) {
405 for (i
= 0; i
< new_maps
; i
++) {
406 ioreq
->page
[i
] = xc_gnttab_map_grant_ref
407 (gnt
, domids
[i
], refs
[i
], ioreq
->prot
);
408 if (ioreq
->page
[i
] == NULL
) {
409 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
410 "can't map grant ref %d (%s, %d maps)\n",
411 refs
[i
], strerror(errno
), ioreq
->blkdev
->cnt_map
);
416 ioreq
->blkdev
->cnt_map
++;
418 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
419 if (page
[i
] == NULL
) {
420 page
[i
] = ioreq
->page
[j
++];
424 if (ioreq
->blkdev
->feature_persistent
) {
425 while ((ioreq
->blkdev
->persistent_gnt_count
< ioreq
->blkdev
->max_grants
)
427 /* Go through the list of newly mapped grants and add as many
428 * as possible to the list of persistently mapped grants.
430 * Since we start at the end of ioreq->page(s), we only need
431 * to decrease new_maps to prevent this granted pages from
432 * being unmapped in ioreq_unmap.
434 grant
= g_malloc0(sizeof(*grant
));
437 grant
->page
= ioreq
->pages
+ (new_maps
) * XC_PAGE_SIZE
;
439 grant
->page
= ioreq
->page
[new_maps
];
441 grant
->blkdev
= ioreq
->blkdev
;
442 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
443 "adding grant %" PRIu32
" page: %p\n",
444 refs
[new_maps
], grant
->page
);
445 g_tree_insert(ioreq
->blkdev
->persistent_gnts
,
446 GUINT_TO_POINTER(refs
[new_maps
]),
448 ioreq
->blkdev
->persistent_gnt_count
++;
451 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
452 ioreq
->v
.iov
[i
].iov_base
+= (uintptr_t)page
[i
];
455 ioreq
->num_unmap
= new_maps
;
459 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
461 static void qemu_aio_complete(void *opaque
, int ret
)
463 struct ioreq
*ioreq
= opaque
;
466 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
467 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
471 ioreq
->aio_inflight
--;
472 if (ioreq
->presync
) {
474 ioreq_runio_qemu_aio(ioreq
);
477 if (ioreq
->aio_inflight
> 0) {
480 if (ioreq
->postsync
) {
482 ioreq
->aio_inflight
++;
483 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
487 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
490 switch (ioreq
->req
.operation
) {
492 case BLKIF_OP_FLUSH_DISKCACHE
:
493 if (!ioreq
->req
.nr_segments
) {
497 block_acct_done(blk_get_stats(ioreq
->blkdev
->blk
), &ioreq
->acct
);
499 case BLKIF_OP_DISCARD
:
503 qemu_bh_schedule(ioreq
->blkdev
->bh
);
506 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
508 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
510 if (ioreq
->req
.nr_segments
&& ioreq_map(ioreq
) == -1) {
514 ioreq
->aio_inflight
++;
515 if (ioreq
->presync
) {
516 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
520 switch (ioreq
->req
.operation
) {
522 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
523 ioreq
->v
.size
, BLOCK_ACCT_READ
);
524 ioreq
->aio_inflight
++;
525 blk_aio_readv(blkdev
->blk
, ioreq
->start
/ BLOCK_SIZE
,
526 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
527 qemu_aio_complete
, ioreq
);
530 case BLKIF_OP_FLUSH_DISKCACHE
:
531 if (!ioreq
->req
.nr_segments
) {
535 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
536 ioreq
->v
.size
, BLOCK_ACCT_WRITE
);
537 ioreq
->aio_inflight
++;
538 blk_aio_writev(blkdev
->blk
, ioreq
->start
/ BLOCK_SIZE
,
539 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
540 qemu_aio_complete
, ioreq
);
542 case BLKIF_OP_DISCARD
:
544 struct blkif_request_discard
*discard_req
= (void *)&ioreq
->req
;
545 ioreq
->aio_inflight
++;
546 blk_aio_discard(blkdev
->blk
,
547 discard_req
->sector_number
, discard_req
->nr_sectors
,
548 qemu_aio_complete
, ioreq
);
552 /* unknown operation (shouldn't happen -- parse catches this) */
556 qemu_aio_complete(ioreq
, 0);
564 ioreq
->status
= BLKIF_RSP_ERROR
;
568 static int blk_send_response_one(struct ioreq
*ioreq
)
570 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
572 int have_requests
= 0;
573 blkif_response_t resp
;
576 resp
.id
= ioreq
->req
.id
;
577 resp
.operation
= ioreq
->req
.operation
;
578 resp
.status
= ioreq
->status
;
580 /* Place on the response ring for the relevant domain. */
581 switch (blkdev
->protocol
) {
582 case BLKIF_PROTOCOL_NATIVE
:
583 dst
= RING_GET_RESPONSE(&blkdev
->rings
.native
, blkdev
->rings
.native
.rsp_prod_pvt
);
585 case BLKIF_PROTOCOL_X86_32
:
586 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_32_part
,
587 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
589 case BLKIF_PROTOCOL_X86_64
:
590 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_64_part
,
591 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
597 memcpy(dst
, &resp
, sizeof(resp
));
598 blkdev
->rings
.common
.rsp_prod_pvt
++;
600 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
601 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
603 * Tail check for pending requests. Allows frontend to avoid
604 * notifications if requests are already in flight (lower
605 * overheads and promotes batching).
607 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
608 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
618 /* walk finished list, send outstanding responses, free requests */
619 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
624 while (!QLIST_EMPTY(&blkdev
->finished
)) {
625 ioreq
= QLIST_FIRST(&blkdev
->finished
);
626 send_notify
+= blk_send_response_one(ioreq
);
627 ioreq_release(ioreq
, true);
630 xen_be_send_notify(&blkdev
->xendev
);
634 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
, RING_IDX rc
)
636 switch (blkdev
->protocol
) {
637 case BLKIF_PROTOCOL_NATIVE
:
638 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
641 case BLKIF_PROTOCOL_X86_32
:
642 blkif_get_x86_32_req(&ioreq
->req
,
643 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
645 case BLKIF_PROTOCOL_X86_64
:
646 blkif_get_x86_64_req(&ioreq
->req
,
647 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
653 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
658 blkdev
->more_work
= 0;
660 rc
= blkdev
->rings
.common
.req_cons
;
661 rp
= blkdev
->rings
.common
.sring
->req_prod
;
662 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
664 blk_send_response_all(blkdev
);
666 /* pull request from ring */
667 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
670 ioreq
= ioreq_start(blkdev
);
675 blk_get_request(blkdev
, ioreq
, rc
);
676 blkdev
->rings
.common
.req_cons
= ++rc
;
679 if (ioreq_parse(ioreq
) != 0) {
680 if (blk_send_response_one(ioreq
)) {
681 xen_be_send_notify(&blkdev
->xendev
);
683 ioreq_release(ioreq
, false);
687 ioreq_runio_qemu_aio(ioreq
);
690 if (blkdev
->more_work
&& blkdev
->requests_inflight
< max_requests
) {
691 qemu_bh_schedule(blkdev
->bh
);
695 /* ------------------------------------------------------------- */
697 static void blk_bh(void *opaque
)
699 struct XenBlkDev
*blkdev
= opaque
;
700 blk_handle_requests(blkdev
);
704 * We need to account for the grant allocations requiring contiguous
705 * chunks; the worst case number would be
706 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
707 * but in order to keep things simple just use
708 * 2 * max_req * max_seg.
710 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
712 static void blk_alloc(struct XenDevice
*xendev
)
714 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
716 QLIST_INIT(&blkdev
->inflight
);
717 QLIST_INIT(&blkdev
->finished
);
718 QLIST_INIT(&blkdev
->freelist
);
719 blkdev
->bh
= qemu_bh_new(blk_bh
, blkdev
);
720 if (xen_mode
!= XEN_EMULATE
) {
723 if (xc_gnttab_set_max_grants(xendev
->gnttabdev
,
724 MAX_GRANTS(max_requests
, BLKIF_MAX_SEGMENTS_PER_REQUEST
)) < 0) {
725 xen_be_printf(xendev
, 0, "xc_gnttab_set_max_grants failed: %s\n",
730 static void blk_parse_discard(struct XenBlkDev
*blkdev
)
734 blkdev
->feature_discard
= true;
736 if (xenstore_read_be_int(&blkdev
->xendev
, "discard-enable", &enable
) == 0) {
737 blkdev
->feature_discard
= !!enable
;
740 if (blkdev
->feature_discard
) {
741 xenstore_write_be_int(&blkdev
->xendev
, "feature-discard", 1);
745 static int blk_init(struct XenDevice
*xendev
)
747 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
749 char *directiosafe
= NULL
;
751 /* read xenstore entries */
752 if (blkdev
->params
== NULL
) {
754 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
755 if (blkdev
->params
!= NULL
) {
756 h
= strchr(blkdev
->params
, ':');
759 blkdev
->fileproto
= blkdev
->params
;
760 blkdev
->filename
= h
+1;
763 blkdev
->fileproto
= "<unset>";
764 blkdev
->filename
= blkdev
->params
;
767 if (!strcmp("aio", blkdev
->fileproto
)) {
768 blkdev
->fileproto
= "raw";
770 if (blkdev
->mode
== NULL
) {
771 blkdev
->mode
= xenstore_read_be_str(&blkdev
->xendev
, "mode");
773 if (blkdev
->type
== NULL
) {
774 blkdev
->type
= xenstore_read_be_str(&blkdev
->xendev
, "type");
776 if (blkdev
->dev
== NULL
) {
777 blkdev
->dev
= xenstore_read_be_str(&blkdev
->xendev
, "dev");
779 if (blkdev
->devtype
== NULL
) {
780 blkdev
->devtype
= xenstore_read_be_str(&blkdev
->xendev
, "device-type");
782 directiosafe
= xenstore_read_be_str(&blkdev
->xendev
, "direct-io-safe");
783 blkdev
->directiosafe
= (directiosafe
&& atoi(directiosafe
));
785 /* do we have all we need? */
786 if (blkdev
->params
== NULL
||
787 blkdev
->mode
== NULL
||
788 blkdev
->type
== NULL
||
789 blkdev
->dev
== NULL
) {
794 if (strcmp(blkdev
->mode
, "w")) {
795 info
|= VDISK_READONLY
;
799 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
803 blkdev
->file_blk
= BLOCK_SIZE
;
806 * blk_connect supplies sector-size and sectors
808 xenstore_write_be_int(&blkdev
->xendev
, "feature-flush-cache", 1);
809 xenstore_write_be_int(&blkdev
->xendev
, "feature-persistent", 1);
810 xenstore_write_be_int(&blkdev
->xendev
, "info", info
);
812 blk_parse_discard(blkdev
);
814 g_free(directiosafe
);
818 g_free(blkdev
->params
);
819 blkdev
->params
= NULL
;
820 g_free(blkdev
->mode
);
822 g_free(blkdev
->type
);
826 g_free(blkdev
->devtype
);
827 blkdev
->devtype
= NULL
;
828 g_free(directiosafe
);
829 blkdev
->directiosafe
= false;
833 static int blk_connect(struct XenDevice
*xendev
)
835 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
836 int pers
, index
, qflags
;
837 bool readonly
= true;
840 if (blkdev
->directiosafe
) {
841 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
843 qflags
= BDRV_O_CACHE_WB
;
845 if (strcmp(blkdev
->mode
, "w") == 0) {
846 qflags
|= BDRV_O_RDWR
;
849 if (blkdev
->feature_discard
) {
850 qflags
|= BDRV_O_UNMAP
;
853 /* init qemu block driver */
854 index
= (blkdev
->xendev
.dev
- 202 * 256) / 16;
855 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
856 if (!blkdev
->dinfo
) {
857 Error
*local_err
= NULL
;
860 BlockDriverState
*bs
;
862 /* setup via xenbus -> create new block driver instance */
863 xen_be_printf(&blkdev
->xendev
, 2, "create new bdrv (xenbus setup)\n");
864 blk
= blk_new_with_bs(blkdev
->dev
, NULL
);
871 drv
= bdrv_find_whitelisted_format(blkdev
->fileproto
, readonly
);
872 if (bdrv_open(&bs
, blkdev
->filename
, NULL
, NULL
, qflags
,
873 drv
, &local_err
) != 0) {
874 xen_be_printf(&blkdev
->xendev
, 0, "error: %s\n",
875 error_get_pretty(local_err
));
876 error_free(local_err
);
881 assert(bs
== blk_bs(blk
));
883 /* setup via qemu cmdline -> already setup for us */
884 xen_be_printf(&blkdev
->xendev
, 2, "get configured bdrv (cmdline setup)\n");
885 blkdev
->blk
= blk_by_legacy_dinfo(blkdev
->dinfo
);
886 if (blk_is_read_only(blkdev
->blk
) && !readonly
) {
887 xen_be_printf(&blkdev
->xendev
, 0, "Unexpected read-only drive");
891 /* blkdev->blk is not create by us, we get a reference
892 * so we can blk_unref() unconditionally */
893 blk_ref(blkdev
->blk
);
895 blk_attach_dev_nofail(blkdev
->blk
, blkdev
);
896 blkdev
->file_size
= blk_getlength(blkdev
->blk
);
897 if (blkdev
->file_size
< 0) {
898 xen_be_printf(&blkdev
->xendev
, 1, "blk_getlength: %d (%s) | drv %s\n",
899 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
900 bdrv_get_format_name(blk_bs(blkdev
->blk
)) ?: "-");
901 blkdev
->file_size
= 0;
904 xen_be_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
905 " size %" PRId64
" (%" PRId64
" MB)\n",
906 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
907 blkdev
->file_size
, blkdev
->file_size
>> 20);
909 /* Fill in number of sector size and number of sectors */
910 xenstore_write_be_int(&blkdev
->xendev
, "sector-size", blkdev
->file_blk
);
911 xenstore_write_be_int64(&blkdev
->xendev
, "sectors",
912 blkdev
->file_size
/ blkdev
->file_blk
);
914 if (xenstore_read_fe_int(&blkdev
->xendev
, "ring-ref", &blkdev
->ring_ref
) == -1) {
917 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
918 &blkdev
->xendev
.remote_port
) == -1) {
921 if (xenstore_read_fe_int(&blkdev
->xendev
, "feature-persistent", &pers
)) {
922 blkdev
->feature_persistent
= FALSE
;
924 blkdev
->feature_persistent
= !!pers
;
927 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
928 if (blkdev
->xendev
.protocol
) {
929 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
930 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
932 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
933 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
937 blkdev
->sring
= xc_gnttab_map_grant_ref(blkdev
->xendev
.gnttabdev
,
940 PROT_READ
| PROT_WRITE
);
941 if (!blkdev
->sring
) {
946 switch (blkdev
->protocol
) {
947 case BLKIF_PROTOCOL_NATIVE
:
949 blkif_sring_t
*sring_native
= blkdev
->sring
;
950 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, XC_PAGE_SIZE
);
953 case BLKIF_PROTOCOL_X86_32
:
955 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
957 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, XC_PAGE_SIZE
);
960 case BLKIF_PROTOCOL_X86_64
:
962 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
964 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, XC_PAGE_SIZE
);
969 if (blkdev
->feature_persistent
) {
970 /* Init persistent grants */
971 blkdev
->max_grants
= max_requests
* BLKIF_MAX_SEGMENTS_PER_REQUEST
;
972 blkdev
->persistent_gnts
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
974 (GDestroyNotify
)destroy_grant
);
975 blkdev
->persistent_gnt_count
= 0;
978 xen_be_bind_evtchn(&blkdev
->xendev
);
980 xen_be_printf(&blkdev
->xendev
, 1, "ok: proto %s, ring-ref %d, "
981 "remote port %d, local port %d\n",
982 blkdev
->xendev
.protocol
, blkdev
->ring_ref
,
983 blkdev
->xendev
.remote_port
, blkdev
->xendev
.local_port
);
987 static void blk_disconnect(struct XenDevice
*xendev
)
989 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
992 blk_detach_dev(blkdev
->blk
, blkdev
);
993 blk_unref(blkdev
->blk
);
996 xen_be_unbind_evtchn(&blkdev
->xendev
);
999 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
1001 blkdev
->sring
= NULL
;
1005 static int blk_free(struct XenDevice
*xendev
)
1007 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1008 struct ioreq
*ioreq
;
1010 if (blkdev
->blk
|| blkdev
->sring
) {
1011 blk_disconnect(xendev
);
1014 /* Free persistent grants */
1015 if (blkdev
->feature_persistent
) {
1016 g_tree_destroy(blkdev
->persistent_gnts
);
1019 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
1020 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
1021 QLIST_REMOVE(ioreq
, list
);
1022 qemu_iovec_destroy(&ioreq
->v
);
1026 g_free(blkdev
->params
);
1027 g_free(blkdev
->mode
);
1028 g_free(blkdev
->type
);
1029 g_free(blkdev
->dev
);
1030 g_free(blkdev
->devtype
);
1031 qemu_bh_delete(blkdev
->bh
);
1035 static void blk_event(struct XenDevice
*xendev
)
1037 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1039 qemu_bh_schedule(blkdev
->bh
);
1042 struct XenDevOps xen_blkdev_ops
= {
1043 .size
= sizeof(struct XenBlkDev
),
1044 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
1047 .initialise
= blk_connect
,
1048 .disconnect
= blk_disconnect
,