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_discard
;
118 gboolean feature_persistent
;
119 GTree
*persistent_gnts
;
120 unsigned int persistent_gnt_count
;
121 unsigned int max_grants
;
123 /* qemu block driver */
125 BlockDriverState
*bs
;
129 /* ------------------------------------------------------------- */
131 static void ioreq_reset(struct ioreq
*ioreq
)
133 memset(&ioreq
->req
, 0, sizeof(ioreq
->req
));
140 memset(ioreq
->domids
, 0, sizeof(ioreq
->domids
));
141 memset(ioreq
->refs
, 0, sizeof(ioreq
->refs
));
143 memset(ioreq
->page
, 0, sizeof(ioreq
->page
));
146 ioreq
->aio_inflight
= 0;
147 ioreq
->aio_errors
= 0;
149 ioreq
->blkdev
= NULL
;
150 memset(&ioreq
->list
, 0, sizeof(ioreq
->list
));
151 memset(&ioreq
->acct
, 0, sizeof(ioreq
->acct
));
153 qemu_iovec_reset(&ioreq
->v
);
156 static gint
int_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
158 uint ua
= GPOINTER_TO_UINT(a
);
159 uint ub
= GPOINTER_TO_UINT(b
);
160 return (ua
> ub
) - (ua
< ub
);
163 static void destroy_grant(gpointer pgnt
)
165 PersistentGrant
*grant
= pgnt
;
166 XenGnttab gnt
= grant
->blkdev
->xendev
.gnttabdev
;
168 if (xc_gnttab_munmap(gnt
, grant
->page
, 1) != 0) {
169 xen_be_printf(&grant
->blkdev
->xendev
, 0,
170 "xc_gnttab_munmap failed: %s\n",
173 grant
->blkdev
->persistent_gnt_count
--;
174 xen_be_printf(&grant
->blkdev
->xendev
, 3,
175 "unmapped grant %p\n", grant
->page
);
179 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
181 struct ioreq
*ioreq
= NULL
;
183 if (QLIST_EMPTY(&blkdev
->freelist
)) {
184 if (blkdev
->requests_total
>= max_requests
) {
187 /* allocate new struct */
188 ioreq
= g_malloc0(sizeof(*ioreq
));
189 ioreq
->blkdev
= blkdev
;
190 blkdev
->requests_total
++;
191 qemu_iovec_init(&ioreq
->v
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
193 /* get one from freelist */
194 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
195 QLIST_REMOVE(ioreq
, list
);
197 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
198 blkdev
->requests_inflight
++;
204 static void ioreq_finish(struct ioreq
*ioreq
)
206 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
208 QLIST_REMOVE(ioreq
, list
);
209 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
210 blkdev
->requests_inflight
--;
211 blkdev
->requests_finished
++;
214 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
216 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
218 QLIST_REMOVE(ioreq
, list
);
220 ioreq
->blkdev
= blkdev
;
221 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
223 blkdev
->requests_finished
--;
225 blkdev
->requests_inflight
--;
230 * translate request into iovec + start offset
231 * do sanity checks along the way
233 static int ioreq_parse(struct ioreq
*ioreq
)
235 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
240 xen_be_printf(&blkdev
->xendev
, 3,
241 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
242 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
243 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
244 switch (ioreq
->req
.operation
) {
246 ioreq
->prot
= PROT_WRITE
; /* to memory */
248 case BLKIF_OP_FLUSH_DISKCACHE
:
250 if (!ioreq
->req
.nr_segments
) {
255 ioreq
->prot
= PROT_READ
; /* from memory */
257 case BLKIF_OP_DISCARD
:
260 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
261 ioreq
->req
.operation
);
265 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
266 xen_be_printf(&blkdev
->xendev
, 0, "error: write req for ro device\n");
270 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
271 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
272 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
273 xen_be_printf(&blkdev
->xendev
, 0, "error: nr_segments too big\n");
276 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
277 xen_be_printf(&blkdev
->xendev
, 0, "error: first > last sector\n");
280 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
281 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
285 ioreq
->domids
[i
] = blkdev
->xendev
.dom
;
286 ioreq
->refs
[i
] = ioreq
->req
.seg
[i
].gref
;
288 mem
= ioreq
->req
.seg
[i
].first_sect
* blkdev
->file_blk
;
289 len
= (ioreq
->req
.seg
[i
].last_sect
- ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
290 qemu_iovec_add(&ioreq
->v
, (void*)mem
, len
);
292 if (ioreq
->start
+ ioreq
->v
.size
> blkdev
->file_size
) {
293 xen_be_printf(&blkdev
->xendev
, 0, "error: access beyond end of file\n");
299 ioreq
->status
= BLKIF_RSP_ERROR
;
303 static void ioreq_unmap(struct ioreq
*ioreq
)
305 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
308 if (ioreq
->num_unmap
== 0 || ioreq
->mapped
== 0) {
315 if (xc_gnttab_munmap(gnt
, ioreq
->pages
, ioreq
->num_unmap
) != 0) {
316 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
319 ioreq
->blkdev
->cnt_map
-= ioreq
->num_unmap
;
322 for (i
= 0; i
< ioreq
->num_unmap
; i
++) {
323 if (!ioreq
->page
[i
]) {
326 if (xc_gnttab_munmap(gnt
, ioreq
->page
[i
], 1) != 0) {
327 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
330 ioreq
->blkdev
->cnt_map
--;
331 ioreq
->page
[i
] = NULL
;
337 static int ioreq_map(struct ioreq
*ioreq
)
339 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
340 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
341 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
342 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
343 int i
, j
, new_maps
= 0;
344 PersistentGrant
*grant
;
345 /* domids and refs variables will contain the information necessary
346 * to map the grants that are needed to fulfill this request.
348 * After mapping the needed grants, the page array will contain the
349 * memory address of each granted page in the order specified in ioreq
350 * (disregarding if it's a persistent grant or not).
353 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 1) {
356 if (ioreq
->blkdev
->feature_persistent
) {
357 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
358 grant
= g_tree_lookup(ioreq
->blkdev
->persistent_gnts
,
359 GUINT_TO_POINTER(ioreq
->refs
[i
]));
362 page
[i
] = grant
->page
;
363 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
364 "using persistent-grant %" PRIu32
"\n",
367 /* Add the grant to the list of grants that
370 domids
[new_maps
] = ioreq
->domids
[i
];
371 refs
[new_maps
] = ioreq
->refs
[i
];
376 /* Set the protection to RW, since grants may be reused later
377 * with a different protection than the one needed for this request
379 ioreq
->prot
= PROT_WRITE
| PROT_READ
;
381 /* All grants in the request should be mapped */
382 memcpy(refs
, ioreq
->refs
, sizeof(refs
));
383 memcpy(domids
, ioreq
->domids
, sizeof(domids
));
384 memset(page
, 0, sizeof(page
));
385 new_maps
= ioreq
->v
.niov
;
388 if (batch_maps
&& new_maps
) {
389 ioreq
->pages
= xc_gnttab_map_grant_refs
390 (gnt
, new_maps
, domids
, refs
, ioreq
->prot
);
391 if (ioreq
->pages
== NULL
) {
392 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
393 "can't map %d grant refs (%s, %d maps)\n",
394 new_maps
, strerror(errno
), ioreq
->blkdev
->cnt_map
);
397 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
398 if (page
[i
] == NULL
) {
399 page
[i
] = ioreq
->pages
+ (j
++) * XC_PAGE_SIZE
;
402 ioreq
->blkdev
->cnt_map
+= new_maps
;
403 } else if (new_maps
) {
404 for (i
= 0; i
< new_maps
; i
++) {
405 ioreq
->page
[i
] = xc_gnttab_map_grant_ref
406 (gnt
, domids
[i
], refs
[i
], ioreq
->prot
);
407 if (ioreq
->page
[i
] == NULL
) {
408 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
409 "can't map grant ref %d (%s, %d maps)\n",
410 refs
[i
], strerror(errno
), ioreq
->blkdev
->cnt_map
);
415 ioreq
->blkdev
->cnt_map
++;
417 for (i
= 0, j
= 0; i
< ioreq
->v
.niov
; i
++) {
418 if (page
[i
] == NULL
) {
419 page
[i
] = ioreq
->page
[j
++];
423 if (ioreq
->blkdev
->feature_persistent
) {
424 while ((ioreq
->blkdev
->persistent_gnt_count
< ioreq
->blkdev
->max_grants
)
426 /* Go through the list of newly mapped grants and add as many
427 * as possible to the list of persistently mapped grants.
429 * Since we start at the end of ioreq->page(s), we only need
430 * to decrease new_maps to prevent this granted pages from
431 * being unmapped in ioreq_unmap.
433 grant
= g_malloc0(sizeof(*grant
));
436 grant
->page
= ioreq
->pages
+ (new_maps
) * XC_PAGE_SIZE
;
438 grant
->page
= ioreq
->page
[new_maps
];
440 grant
->blkdev
= ioreq
->blkdev
;
441 xen_be_printf(&ioreq
->blkdev
->xendev
, 3,
442 "adding grant %" PRIu32
" page: %p\n",
443 refs
[new_maps
], grant
->page
);
444 g_tree_insert(ioreq
->blkdev
->persistent_gnts
,
445 GUINT_TO_POINTER(refs
[new_maps
]),
447 ioreq
->blkdev
->persistent_gnt_count
++;
450 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
451 ioreq
->v
.iov
[i
].iov_base
+= (uintptr_t)page
[i
];
454 ioreq
->num_unmap
= new_maps
;
458 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
460 static void qemu_aio_complete(void *opaque
, int ret
)
462 struct ioreq
*ioreq
= opaque
;
465 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
466 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
470 ioreq
->aio_inflight
--;
471 if (ioreq
->presync
) {
473 ioreq_runio_qemu_aio(ioreq
);
476 if (ioreq
->aio_inflight
> 0) {
479 if (ioreq
->postsync
) {
481 ioreq
->aio_inflight
++;
482 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
486 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
489 switch (ioreq
->req
.operation
) {
491 case BLKIF_OP_FLUSH_DISKCACHE
:
492 if (!ioreq
->req
.nr_segments
) {
496 block_acct_done(bdrv_get_stats(ioreq
->blkdev
->bs
), &ioreq
->acct
);
498 case BLKIF_OP_DISCARD
:
502 qemu_bh_schedule(ioreq
->blkdev
->bh
);
505 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
507 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
509 if (ioreq
->req
.nr_segments
&& ioreq_map(ioreq
) == -1) {
513 ioreq
->aio_inflight
++;
514 if (ioreq
->presync
) {
515 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
519 switch (ioreq
->req
.operation
) {
521 block_acct_start(bdrv_get_stats(blkdev
->bs
), &ioreq
->acct
,
522 ioreq
->v
.size
, BLOCK_ACCT_READ
);
523 ioreq
->aio_inflight
++;
524 bdrv_aio_readv(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
525 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
526 qemu_aio_complete
, ioreq
);
529 case BLKIF_OP_FLUSH_DISKCACHE
:
530 if (!ioreq
->req
.nr_segments
) {
534 block_acct_start(bdrv_get_stats(blkdev
->bs
), &ioreq
->acct
,
535 ioreq
->v
.size
, BLOCK_ACCT_WRITE
);
536 ioreq
->aio_inflight
++;
537 bdrv_aio_writev(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
538 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
539 qemu_aio_complete
, ioreq
);
541 case BLKIF_OP_DISCARD
:
543 struct blkif_request_discard
*discard_req
= (void *)&ioreq
->req
;
544 ioreq
->aio_inflight
++;
545 bdrv_aio_discard(blkdev
->bs
,
546 discard_req
->sector_number
, discard_req
->nr_sectors
,
547 qemu_aio_complete
, ioreq
);
551 /* unknown operation (shouldn't happen -- parse catches this) */
555 qemu_aio_complete(ioreq
, 0);
563 ioreq
->status
= BLKIF_RSP_ERROR
;
567 static int blk_send_response_one(struct ioreq
*ioreq
)
569 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
571 int have_requests
= 0;
572 blkif_response_t resp
;
575 resp
.id
= ioreq
->req
.id
;
576 resp
.operation
= ioreq
->req
.operation
;
577 resp
.status
= ioreq
->status
;
579 /* Place on the response ring for the relevant domain. */
580 switch (blkdev
->protocol
) {
581 case BLKIF_PROTOCOL_NATIVE
:
582 dst
= RING_GET_RESPONSE(&blkdev
->rings
.native
, blkdev
->rings
.native
.rsp_prod_pvt
);
584 case BLKIF_PROTOCOL_X86_32
:
585 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_32_part
,
586 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
588 case BLKIF_PROTOCOL_X86_64
:
589 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_64_part
,
590 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
596 memcpy(dst
, &resp
, sizeof(resp
));
597 blkdev
->rings
.common
.rsp_prod_pvt
++;
599 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
600 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
602 * Tail check for pending requests. Allows frontend to avoid
603 * notifications if requests are already in flight (lower
604 * overheads and promotes batching).
606 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
607 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
617 /* walk finished list, send outstanding responses, free requests */
618 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
623 while (!QLIST_EMPTY(&blkdev
->finished
)) {
624 ioreq
= QLIST_FIRST(&blkdev
->finished
);
625 send_notify
+= blk_send_response_one(ioreq
);
626 ioreq_release(ioreq
, true);
629 xen_be_send_notify(&blkdev
->xendev
);
633 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
, RING_IDX rc
)
635 switch (blkdev
->protocol
) {
636 case BLKIF_PROTOCOL_NATIVE
:
637 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
640 case BLKIF_PROTOCOL_X86_32
:
641 blkif_get_x86_32_req(&ioreq
->req
,
642 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
644 case BLKIF_PROTOCOL_X86_64
:
645 blkif_get_x86_64_req(&ioreq
->req
,
646 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
652 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
657 blkdev
->more_work
= 0;
659 rc
= blkdev
->rings
.common
.req_cons
;
660 rp
= blkdev
->rings
.common
.sring
->req_prod
;
661 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
663 blk_send_response_all(blkdev
);
665 /* pull request from ring */
666 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
669 ioreq
= ioreq_start(blkdev
);
674 blk_get_request(blkdev
, ioreq
, rc
);
675 blkdev
->rings
.common
.req_cons
= ++rc
;
678 if (ioreq_parse(ioreq
) != 0) {
679 if (blk_send_response_one(ioreq
)) {
680 xen_be_send_notify(&blkdev
->xendev
);
682 ioreq_release(ioreq
, false);
686 ioreq_runio_qemu_aio(ioreq
);
689 if (blkdev
->more_work
&& blkdev
->requests_inflight
< max_requests
) {
690 qemu_bh_schedule(blkdev
->bh
);
694 /* ------------------------------------------------------------- */
696 static void blk_bh(void *opaque
)
698 struct XenBlkDev
*blkdev
= opaque
;
699 blk_handle_requests(blkdev
);
703 * We need to account for the grant allocations requiring contiguous
704 * chunks; the worst case number would be
705 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
706 * but in order to keep things simple just use
707 * 2 * max_req * max_seg.
709 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
711 static void blk_alloc(struct XenDevice
*xendev
)
713 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
715 QLIST_INIT(&blkdev
->inflight
);
716 QLIST_INIT(&blkdev
->finished
);
717 QLIST_INIT(&blkdev
->freelist
);
718 blkdev
->bh
= qemu_bh_new(blk_bh
, blkdev
);
719 if (xen_mode
!= XEN_EMULATE
) {
722 if (xc_gnttab_set_max_grants(xendev
->gnttabdev
,
723 MAX_GRANTS(max_requests
, BLKIF_MAX_SEGMENTS_PER_REQUEST
)) < 0) {
724 xen_be_printf(xendev
, 0, "xc_gnttab_set_max_grants failed: %s\n",
729 static void blk_parse_discard(struct XenBlkDev
*blkdev
)
733 blkdev
->feature_discard
= true;
735 if (xenstore_read_be_int(&blkdev
->xendev
, "discard-enable", &enable
) == 0) {
736 blkdev
->feature_discard
= !!enable
;
739 if (blkdev
->feature_discard
) {
740 xenstore_write_be_int(&blkdev
->xendev
, "feature-discard", 1);
744 static int blk_init(struct XenDevice
*xendev
)
746 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
748 char *directiosafe
= NULL
;
750 /* read xenstore entries */
751 if (blkdev
->params
== NULL
) {
753 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
754 if (blkdev
->params
!= NULL
) {
755 h
= strchr(blkdev
->params
, ':');
758 blkdev
->fileproto
= blkdev
->params
;
759 blkdev
->filename
= h
+1;
762 blkdev
->fileproto
= "<unset>";
763 blkdev
->filename
= blkdev
->params
;
766 if (!strcmp("aio", blkdev
->fileproto
)) {
767 blkdev
->fileproto
= "raw";
769 if (blkdev
->mode
== NULL
) {
770 blkdev
->mode
= xenstore_read_be_str(&blkdev
->xendev
, "mode");
772 if (blkdev
->type
== NULL
) {
773 blkdev
->type
= xenstore_read_be_str(&blkdev
->xendev
, "type");
775 if (blkdev
->dev
== NULL
) {
776 blkdev
->dev
= xenstore_read_be_str(&blkdev
->xendev
, "dev");
778 if (blkdev
->devtype
== NULL
) {
779 blkdev
->devtype
= xenstore_read_be_str(&blkdev
->xendev
, "device-type");
781 directiosafe
= xenstore_read_be_str(&blkdev
->xendev
, "direct-io-safe");
782 blkdev
->directiosafe
= (directiosafe
&& atoi(directiosafe
));
784 /* do we have all we need? */
785 if (blkdev
->params
== NULL
||
786 blkdev
->mode
== NULL
||
787 blkdev
->type
== NULL
||
788 blkdev
->dev
== NULL
) {
793 if (strcmp(blkdev
->mode
, "w")) {
794 info
|= VDISK_READONLY
;
798 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
802 blkdev
->file_blk
= BLOCK_SIZE
;
805 * blk_connect supplies sector-size and sectors
807 xenstore_write_be_int(&blkdev
->xendev
, "feature-flush-cache", 1);
808 xenstore_write_be_int(&blkdev
->xendev
, "feature-persistent", 1);
809 xenstore_write_be_int(&blkdev
->xendev
, "info", info
);
811 blk_parse_discard(blkdev
);
813 g_free(directiosafe
);
817 g_free(blkdev
->params
);
818 blkdev
->params
= NULL
;
819 g_free(blkdev
->mode
);
821 g_free(blkdev
->type
);
825 g_free(blkdev
->devtype
);
826 blkdev
->devtype
= NULL
;
827 g_free(directiosafe
);
828 blkdev
->directiosafe
= false;
832 static int blk_connect(struct XenDevice
*xendev
)
834 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
835 int pers
, index
, qflags
;
836 bool readonly
= true;
839 if (blkdev
->directiosafe
) {
840 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
842 qflags
= BDRV_O_CACHE_WB
;
844 if (strcmp(blkdev
->mode
, "w") == 0) {
845 qflags
|= BDRV_O_RDWR
;
848 if (blkdev
->feature_discard
) {
849 qflags
|= BDRV_O_UNMAP
;
852 /* init qemu block driver */
853 index
= (blkdev
->xendev
.dev
- 202 * 256) / 16;
854 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
855 if (!blkdev
->dinfo
) {
856 Error
*local_err
= NULL
;
859 /* setup via xenbus -> create new block driver instance */
860 xen_be_printf(&blkdev
->xendev
, 2, "create new bdrv (xenbus setup)\n");
861 blkdev
->bs
= bdrv_new(blkdev
->dev
, NULL
);
866 drv
= bdrv_find_whitelisted_format(blkdev
->fileproto
, readonly
);
867 if (bdrv_open(&blkdev
->bs
, blkdev
->filename
, NULL
, NULL
, qflags
,
868 drv
, &local_err
) != 0) {
869 xen_be_printf(&blkdev
->xendev
, 0, "error: %s\n",
870 error_get_pretty(local_err
));
871 error_free(local_err
);
872 bdrv_unref(blkdev
->bs
);
877 /* setup via qemu cmdline -> already setup for us */
878 xen_be_printf(&blkdev
->xendev
, 2, "get configured bdrv (cmdline setup)\n");
879 blkdev
->bs
= blkdev
->dinfo
->bdrv
;
880 if (bdrv_is_read_only(blkdev
->bs
) && !readonly
) {
881 xen_be_printf(&blkdev
->xendev
, 0, "Unexpected read-only drive");
885 /* blkdev->bs is not create by us, we get a reference
886 * so we can bdrv_unref() unconditionally */
887 bdrv_ref(blkdev
->bs
);
889 bdrv_attach_dev_nofail(blkdev
->bs
, blkdev
);
890 blkdev
->file_size
= bdrv_getlength(blkdev
->bs
);
891 if (blkdev
->file_size
< 0) {
892 xen_be_printf(&blkdev
->xendev
, 1, "bdrv_getlength: %d (%s) | drv %s\n",
893 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
894 bdrv_get_format_name(blkdev
->bs
) ?: "-");
895 blkdev
->file_size
= 0;
898 xen_be_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
899 " size %" PRId64
" (%" PRId64
" MB)\n",
900 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
901 blkdev
->file_size
, blkdev
->file_size
>> 20);
903 /* Fill in number of sector size and number of sectors */
904 xenstore_write_be_int(&blkdev
->xendev
, "sector-size", blkdev
->file_blk
);
905 xenstore_write_be_int64(&blkdev
->xendev
, "sectors",
906 blkdev
->file_size
/ blkdev
->file_blk
);
908 if (xenstore_read_fe_int(&blkdev
->xendev
, "ring-ref", &blkdev
->ring_ref
) == -1) {
911 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
912 &blkdev
->xendev
.remote_port
) == -1) {
915 if (xenstore_read_fe_int(&blkdev
->xendev
, "feature-persistent", &pers
)) {
916 blkdev
->feature_persistent
= FALSE
;
918 blkdev
->feature_persistent
= !!pers
;
921 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
922 if (blkdev
->xendev
.protocol
) {
923 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
924 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
926 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
927 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
931 blkdev
->sring
= xc_gnttab_map_grant_ref(blkdev
->xendev
.gnttabdev
,
934 PROT_READ
| PROT_WRITE
);
935 if (!blkdev
->sring
) {
940 switch (blkdev
->protocol
) {
941 case BLKIF_PROTOCOL_NATIVE
:
943 blkif_sring_t
*sring_native
= blkdev
->sring
;
944 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, XC_PAGE_SIZE
);
947 case BLKIF_PROTOCOL_X86_32
:
949 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
951 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, XC_PAGE_SIZE
);
954 case BLKIF_PROTOCOL_X86_64
:
956 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
958 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, XC_PAGE_SIZE
);
963 if (blkdev
->feature_persistent
) {
964 /* Init persistent grants */
965 blkdev
->max_grants
= max_requests
* BLKIF_MAX_SEGMENTS_PER_REQUEST
;
966 blkdev
->persistent_gnts
= g_tree_new_full((GCompareDataFunc
)int_cmp
,
968 (GDestroyNotify
)destroy_grant
);
969 blkdev
->persistent_gnt_count
= 0;
972 xen_be_bind_evtchn(&blkdev
->xendev
);
974 xen_be_printf(&blkdev
->xendev
, 1, "ok: proto %s, ring-ref %d, "
975 "remote port %d, local port %d\n",
976 blkdev
->xendev
.protocol
, blkdev
->ring_ref
,
977 blkdev
->xendev
.remote_port
, blkdev
->xendev
.local_port
);
981 static void blk_disconnect(struct XenDevice
*xendev
)
983 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
986 bdrv_detach_dev(blkdev
->bs
, blkdev
);
987 bdrv_unref(blkdev
->bs
);
990 xen_be_unbind_evtchn(&blkdev
->xendev
);
993 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
995 blkdev
->sring
= NULL
;
999 static int blk_free(struct XenDevice
*xendev
)
1001 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1002 struct ioreq
*ioreq
;
1004 if (blkdev
->bs
|| blkdev
->sring
) {
1005 blk_disconnect(xendev
);
1008 /* Free persistent grants */
1009 if (blkdev
->feature_persistent
) {
1010 g_tree_destroy(blkdev
->persistent_gnts
);
1013 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
1014 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
1015 QLIST_REMOVE(ioreq
, list
);
1016 qemu_iovec_destroy(&ioreq
->v
);
1020 g_free(blkdev
->params
);
1021 g_free(blkdev
->mode
);
1022 g_free(blkdev
->type
);
1023 g_free(blkdev
->dev
);
1024 g_free(blkdev
->devtype
);
1025 qemu_bh_delete(blkdev
->bh
);
1029 static void blk_event(struct XenDevice
*xendev
)
1031 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1033 qemu_bh_schedule(blkdev
->bh
);
1036 struct XenDevOps xen_blkdev_ops
= {
1037 .size
= sizeof(struct XenBlkDev
),
1038 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
1041 .initialise
= blk_connect
,
1042 .disconnect
= blk_disconnect
,