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.
22 #include "qemu/osdep.h"
23 #include "qemu/units.h"
24 #include <sys/ioctl.h>
28 #include "hw/xen/xen_backend.h"
29 #include "xen_blkif.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/iothread.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
38 /* ------------------------------------------------------------- */
40 #define BLOCK_SIZE 512
41 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
58 struct XenBlkDev
*blkdev
;
59 QLIST_ENTRY(ioreq
) list
;
63 #define MAX_RING_PAGE_ORDER 4
66 struct XenLegacyDevice xendev
; /* must be first */
73 const char *fileproto
;
75 unsigned int ring_ref
[1 << MAX_RING_PAGE_ORDER
];
76 unsigned int nr_ring_ref
;
81 blkif_back_rings_t rings
;
85 QLIST_HEAD(inflight_head
, ioreq
) inflight
;
86 QLIST_HEAD(finished_head
, ioreq
) finished
;
87 QLIST_HEAD(freelist_head
, ioreq
) freelist
;
89 int requests_inflight
;
90 int requests_finished
;
91 unsigned int max_requests
;
93 gboolean feature_discard
;
95 /* qemu block driver */
104 /* ------------------------------------------------------------- */
106 static void ioreq_reset(struct ioreq
*ioreq
)
108 memset(&ioreq
->req
, 0, sizeof(ioreq
->req
));
115 ioreq
->aio_inflight
= 0;
116 ioreq
->aio_errors
= 0;
118 ioreq
->blkdev
= NULL
;
119 memset(&ioreq
->list
, 0, sizeof(ioreq
->list
));
120 memset(&ioreq
->acct
, 0, sizeof(ioreq
->acct
));
122 qemu_iovec_reset(&ioreq
->v
);
125 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
127 struct ioreq
*ioreq
= NULL
;
129 if (QLIST_EMPTY(&blkdev
->freelist
)) {
130 if (blkdev
->requests_total
>= blkdev
->max_requests
) {
133 /* allocate new struct */
134 ioreq
= g_malloc0(sizeof(*ioreq
));
135 ioreq
->blkdev
= blkdev
;
136 blkdev
->requests_total
++;
137 qemu_iovec_init(&ioreq
->v
, 1);
139 /* get one from freelist */
140 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
141 QLIST_REMOVE(ioreq
, list
);
143 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
144 blkdev
->requests_inflight
++;
150 static void ioreq_finish(struct ioreq
*ioreq
)
152 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
154 QLIST_REMOVE(ioreq
, list
);
155 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
156 blkdev
->requests_inflight
--;
157 blkdev
->requests_finished
++;
160 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
162 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
164 QLIST_REMOVE(ioreq
, list
);
166 ioreq
->blkdev
= blkdev
;
167 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
169 blkdev
->requests_finished
--;
171 blkdev
->requests_inflight
--;
176 * translate request into iovec + start offset
177 * do sanity checks along the way
179 static int ioreq_parse(struct ioreq
*ioreq
)
181 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
182 struct XenLegacyDevice
*xendev
= &blkdev
->xendev
;
188 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
189 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
190 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
191 switch (ioreq
->req
.operation
) {
194 case BLKIF_OP_FLUSH_DISKCACHE
:
196 if (!ioreq
->req
.nr_segments
) {
202 case BLKIF_OP_DISCARD
:
205 xen_pv_printf(xendev
, 0, "error: unknown operation (%d)\n",
206 ioreq
->req
.operation
);
210 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
211 xen_pv_printf(xendev
, 0, "error: write req for ro device\n");
215 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
216 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
217 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
218 xen_pv_printf(xendev
, 0, "error: nr_segments too big\n");
221 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
222 xen_pv_printf(xendev
, 0, "error: first > last sector\n");
225 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
226 xen_pv_printf(xendev
, 0, "error: page crossing\n");
230 len
= (ioreq
->req
.seg
[i
].last_sect
-
231 ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
234 if (ioreq
->start
+ ioreq
->size
> blkdev
->file_size
) {
235 xen_pv_printf(xendev
, 0, "error: access beyond end of file\n");
241 ioreq
->status
= BLKIF_RSP_ERROR
;
245 static int ioreq_grant_copy(struct ioreq
*ioreq
)
247 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
248 struct XenLegacyDevice
*xendev
= &blkdev
->xendev
;
249 XenGrantCopySegment segs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
251 int64_t file_blk
= blkdev
->file_blk
;
252 bool to_domain
= (ioreq
->req
.operation
== BLKIF_OP_READ
);
253 void *virt
= ioreq
->buf
;
255 if (ioreq
->req
.nr_segments
== 0) {
259 count
= ioreq
->req
.nr_segments
;
261 for (i
= 0; i
< count
; i
++) {
263 segs
[i
].dest
.foreign
.ref
= ioreq
->req
.seg
[i
].gref
;
264 segs
[i
].dest
.foreign
.offset
= ioreq
->req
.seg
[i
].first_sect
*
266 segs
[i
].source
.virt
= virt
;
268 segs
[i
].source
.foreign
.ref
= ioreq
->req
.seg
[i
].gref
;
269 segs
[i
].source
.foreign
.offset
= ioreq
->req
.seg
[i
].first_sect
*
271 segs
[i
].dest
.virt
= virt
;
273 segs
[i
].len
= (ioreq
->req
.seg
[i
].last_sect
274 - ioreq
->req
.seg
[i
].first_sect
+ 1) * file_blk
;
278 rc
= xen_be_copy_grant_refs(xendev
, to_domain
, segs
, count
);
281 xen_pv_printf(xendev
, 0,
282 "failed to copy data %d\n", rc
);
290 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
292 static void qemu_aio_complete(void *opaque
, int ret
)
294 struct ioreq
*ioreq
= opaque
;
295 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
296 struct XenLegacyDevice
*xendev
= &blkdev
->xendev
;
298 aio_context_acquire(blkdev
->ctx
);
301 xen_pv_printf(xendev
, 0, "%s I/O error\n",
302 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
306 ioreq
->aio_inflight
--;
307 if (ioreq
->presync
) {
309 ioreq_runio_qemu_aio(ioreq
);
312 if (ioreq
->aio_inflight
> 0) {
316 switch (ioreq
->req
.operation
) {
318 /* in case of failure ioreq->aio_errors is increased */
320 ioreq_grant_copy(ioreq
);
322 qemu_vfree(ioreq
->buf
);
325 case BLKIF_OP_FLUSH_DISKCACHE
:
326 if (!ioreq
->req
.nr_segments
) {
329 qemu_vfree(ioreq
->buf
);
335 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
338 switch (ioreq
->req
.operation
) {
340 case BLKIF_OP_FLUSH_DISKCACHE
:
341 if (!ioreq
->req
.nr_segments
) {
345 if (ioreq
->status
== BLKIF_RSP_OKAY
) {
346 block_acct_done(blk_get_stats(blkdev
->blk
), &ioreq
->acct
);
348 block_acct_failed(blk_get_stats(blkdev
->blk
), &ioreq
->acct
);
351 case BLKIF_OP_DISCARD
:
355 qemu_bh_schedule(blkdev
->bh
);
358 aio_context_release(blkdev
->ctx
);
361 static bool blk_split_discard(struct ioreq
*ioreq
, blkif_sector_t sector_number
,
364 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
367 uint64_t byte_remaining
, limit
;
368 uint64_t sec_start
= sector_number
;
369 uint64_t sec_count
= nr_sectors
;
371 /* Wrap around, or overflowing byte limit? */
372 if (sec_start
+ sec_count
< sec_count
||
373 sec_start
+ sec_count
> INT64_MAX
>> BDRV_SECTOR_BITS
) {
377 limit
= BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
;
378 byte_offset
= sec_start
<< BDRV_SECTOR_BITS
;
379 byte_remaining
= sec_count
<< BDRV_SECTOR_BITS
;
382 byte_chunk
= byte_remaining
> limit
? limit
: byte_remaining
;
383 ioreq
->aio_inflight
++;
384 blk_aio_pdiscard(blkdev
->blk
, byte_offset
, byte_chunk
,
385 qemu_aio_complete
, ioreq
);
386 byte_remaining
-= byte_chunk
;
387 byte_offset
+= byte_chunk
;
388 } while (byte_remaining
> 0);
393 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
395 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
397 ioreq
->buf
= qemu_memalign(XC_PAGE_SIZE
, ioreq
->size
);
398 if (ioreq
->req
.nr_segments
&&
399 (ioreq
->req
.operation
== BLKIF_OP_WRITE
||
400 ioreq
->req
.operation
== BLKIF_OP_FLUSH_DISKCACHE
) &&
401 ioreq_grant_copy(ioreq
)) {
402 qemu_vfree(ioreq
->buf
);
406 ioreq
->aio_inflight
++;
407 if (ioreq
->presync
) {
408 blk_aio_flush(ioreq
->blkdev
->blk
, qemu_aio_complete
, ioreq
);
412 switch (ioreq
->req
.operation
) {
414 qemu_iovec_add(&ioreq
->v
, ioreq
->buf
, ioreq
->size
);
415 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
416 ioreq
->v
.size
, BLOCK_ACCT_READ
);
417 ioreq
->aio_inflight
++;
418 blk_aio_preadv(blkdev
->blk
, ioreq
->start
, &ioreq
->v
, 0,
419 qemu_aio_complete
, ioreq
);
422 case BLKIF_OP_FLUSH_DISKCACHE
:
423 if (!ioreq
->req
.nr_segments
) {
427 qemu_iovec_add(&ioreq
->v
, ioreq
->buf
, ioreq
->size
);
428 block_acct_start(blk_get_stats(blkdev
->blk
), &ioreq
->acct
,
430 ioreq
->req
.operation
== BLKIF_OP_WRITE
?
431 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
432 ioreq
->aio_inflight
++;
433 blk_aio_pwritev(blkdev
->blk
, ioreq
->start
, &ioreq
->v
, 0,
434 qemu_aio_complete
, ioreq
);
436 case BLKIF_OP_DISCARD
:
438 struct blkif_request_discard
*req
= (void *)&ioreq
->req
;
439 if (!blk_split_discard(ioreq
, req
->sector_number
, req
->nr_sectors
)) {
445 /* unknown operation (shouldn't happen -- parse catches this) */
449 qemu_aio_complete(ioreq
, 0);
455 ioreq
->status
= BLKIF_RSP_ERROR
;
459 static int blk_send_response_one(struct ioreq
*ioreq
)
461 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
463 int have_requests
= 0;
464 blkif_response_t
*resp
;
466 /* Place on the response ring for the relevant domain. */
467 switch (blkdev
->protocol
) {
468 case BLKIF_PROTOCOL_NATIVE
:
469 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
470 &blkdev
->rings
.native
,
471 blkdev
->rings
.native
.rsp_prod_pvt
);
473 case BLKIF_PROTOCOL_X86_32
:
474 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
475 &blkdev
->rings
.x86_32_part
,
476 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
478 case BLKIF_PROTOCOL_X86_64
:
479 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
480 &blkdev
->rings
.x86_64_part
,
481 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
487 resp
->id
= ioreq
->req
.id
;
488 resp
->operation
= ioreq
->req
.operation
;
489 resp
->status
= ioreq
->status
;
491 blkdev
->rings
.common
.rsp_prod_pvt
++;
493 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
494 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
496 * Tail check for pending requests. Allows frontend to avoid
497 * notifications if requests are already in flight (lower
498 * overheads and promotes batching).
500 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
501 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
511 /* walk finished list, send outstanding responses, free requests */
512 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
517 while (!QLIST_EMPTY(&blkdev
->finished
)) {
518 ioreq
= QLIST_FIRST(&blkdev
->finished
);
519 send_notify
+= blk_send_response_one(ioreq
);
520 ioreq_release(ioreq
, true);
523 xen_pv_send_notify(&blkdev
->xendev
);
527 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
,
530 switch (blkdev
->protocol
) {
531 case BLKIF_PROTOCOL_NATIVE
:
532 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
535 case BLKIF_PROTOCOL_X86_32
:
536 blkif_get_x86_32_req(&ioreq
->req
,
537 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
539 case BLKIF_PROTOCOL_X86_64
:
540 blkif_get_x86_64_req(&ioreq
->req
,
541 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
544 /* Prevent the compiler from accessing the on-ring fields instead. */
549 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
554 blkdev
->more_work
= 0;
556 rc
= blkdev
->rings
.common
.req_cons
;
557 rp
= blkdev
->rings
.common
.sring
->req_prod
;
558 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
560 blk_send_response_all(blkdev
);
562 /* pull request from ring */
563 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
566 ioreq
= ioreq_start(blkdev
);
571 blk_get_request(blkdev
, ioreq
, rc
);
572 blkdev
->rings
.common
.req_cons
= ++rc
;
575 if (ioreq_parse(ioreq
) != 0) {
577 switch (ioreq
->req
.operation
) {
579 block_acct_invalid(blk_get_stats(blkdev
->blk
),
583 block_acct_invalid(blk_get_stats(blkdev
->blk
),
586 case BLKIF_OP_FLUSH_DISKCACHE
:
587 block_acct_invalid(blk_get_stats(blkdev
->blk
),
593 if (blk_send_response_one(ioreq
)) {
594 xen_pv_send_notify(&blkdev
->xendev
);
596 ioreq_release(ioreq
, false);
600 ioreq_runio_qemu_aio(ioreq
);
603 if (blkdev
->more_work
&& blkdev
->requests_inflight
< blkdev
->max_requests
) {
604 qemu_bh_schedule(blkdev
->bh
);
608 /* ------------------------------------------------------------- */
610 static void blk_bh(void *opaque
)
612 struct XenBlkDev
*blkdev
= opaque
;
614 aio_context_acquire(blkdev
->ctx
);
615 blk_handle_requests(blkdev
);
616 aio_context_release(blkdev
->ctx
);
619 static void blk_alloc(struct XenLegacyDevice
*xendev
)
621 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
624 trace_xen_disk_alloc(xendev
->name
);
626 QLIST_INIT(&blkdev
->inflight
);
627 QLIST_INIT(&blkdev
->finished
);
628 QLIST_INIT(&blkdev
->freelist
);
630 blkdev
->iothread
= iothread_create(xendev
->name
, &err
);
633 blkdev
->ctx
= iothread_get_aio_context(blkdev
->iothread
);
634 blkdev
->bh
= aio_bh_new(blkdev
->ctx
, blk_bh
, blkdev
);
637 static void blk_parse_discard(struct XenBlkDev
*blkdev
)
639 struct XenLegacyDevice
*xendev
= &blkdev
->xendev
;
642 blkdev
->feature_discard
= true;
644 if (xenstore_read_be_int(xendev
, "discard-enable", &enable
) == 0) {
645 blkdev
->feature_discard
= !!enable
;
648 if (blkdev
->feature_discard
) {
649 xenstore_write_be_int(xendev
, "feature-discard", 1);
653 static int blk_init(struct XenLegacyDevice
*xendev
)
655 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
657 char *directiosafe
= NULL
;
659 trace_xen_disk_init(xendev
->name
);
661 /* read xenstore entries */
662 if (blkdev
->params
== NULL
) {
664 blkdev
->params
= xenstore_read_be_str(xendev
, "params");
665 if (blkdev
->params
!= NULL
) {
666 h
= strchr(blkdev
->params
, ':');
669 blkdev
->fileproto
= blkdev
->params
;
670 blkdev
->filename
= h
+ 1;
673 blkdev
->fileproto
= "<unset>";
674 blkdev
->filename
= blkdev
->params
;
677 if (!strcmp("aio", blkdev
->fileproto
)) {
678 blkdev
->fileproto
= "raw";
680 if (!strcmp("vhd", blkdev
->fileproto
)) {
681 blkdev
->fileproto
= "vpc";
683 if (blkdev
->mode
== NULL
) {
684 blkdev
->mode
= xenstore_read_be_str(xendev
, "mode");
686 if (blkdev
->type
== NULL
) {
687 blkdev
->type
= xenstore_read_be_str(xendev
, "type");
689 if (blkdev
->dev
== NULL
) {
690 blkdev
->dev
= xenstore_read_be_str(xendev
, "dev");
692 if (blkdev
->devtype
== NULL
) {
693 blkdev
->devtype
= xenstore_read_be_str(xendev
, "device-type");
695 directiosafe
= xenstore_read_be_str(xendev
, "direct-io-safe");
696 blkdev
->directiosafe
= (directiosafe
&& atoi(directiosafe
));
698 /* do we have all we need? */
699 if (blkdev
->params
== NULL
||
700 blkdev
->mode
== NULL
||
701 blkdev
->type
== NULL
||
702 blkdev
->dev
== NULL
) {
707 if (strcmp(blkdev
->mode
, "w")) {
708 info
|= VDISK_READONLY
;
712 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
716 blkdev
->file_blk
= BLOCK_SIZE
;
719 * blk_connect supplies sector-size and sectors
721 xenstore_write_be_int(xendev
, "feature-flush-cache", 1);
722 xenstore_write_be_int(xendev
, "info", info
);
724 xenstore_write_be_int(xendev
, "max-ring-page-order",
725 MAX_RING_PAGE_ORDER
);
727 blk_parse_discard(blkdev
);
729 g_free(directiosafe
);
733 g_free(blkdev
->params
);
734 blkdev
->params
= NULL
;
735 g_free(blkdev
->mode
);
737 g_free(blkdev
->type
);
741 g_free(blkdev
->devtype
);
742 blkdev
->devtype
= NULL
;
743 g_free(directiosafe
);
744 blkdev
->directiosafe
= false;
748 static int blk_connect(struct XenLegacyDevice
*xendev
)
750 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
752 bool readonly
= true;
753 bool writethrough
= true;
755 unsigned int ring_size
, max_grants
;
758 trace_xen_disk_connect(xendev
->name
);
761 if (blkdev
->directiosafe
) {
762 qflags
= BDRV_O_NOCACHE
| BDRV_O_NATIVE_AIO
;
765 writethrough
= false;
767 if (strcmp(blkdev
->mode
, "w") == 0) {
768 qflags
|= BDRV_O_RDWR
;
771 if (blkdev
->feature_discard
) {
772 qflags
|= BDRV_O_UNMAP
;
775 /* init qemu block driver */
776 index
= (xendev
->dev
- 202 * 256) / 16;
777 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
778 if (!blkdev
->dinfo
) {
779 Error
*local_err
= NULL
;
780 QDict
*options
= NULL
;
782 if (strcmp(blkdev
->fileproto
, "<unset>")) {
783 options
= qdict_new();
784 qdict_put_str(options
, "driver", blkdev
->fileproto
);
787 /* setup via xenbus -> create new block driver instance */
788 xen_pv_printf(xendev
, 2, "create new bdrv (xenbus setup)\n");
789 blkdev
->blk
= blk_new_open(blkdev
->filename
, NULL
, options
,
792 xen_pv_printf(xendev
, 0, "error: %s\n",
793 error_get_pretty(local_err
));
794 error_free(local_err
);
797 blk_set_enable_write_cache(blkdev
->blk
, !writethrough
);
799 /* setup via qemu cmdline -> already setup for us */
800 xen_pv_printf(xendev
, 2,
801 "get configured bdrv (cmdline setup)\n");
802 blkdev
->blk
= blk_by_legacy_dinfo(blkdev
->dinfo
);
803 if (blk_is_read_only(blkdev
->blk
) && !readonly
) {
804 xen_pv_printf(xendev
, 0, "Unexpected read-only drive");
808 /* blkdev->blk is not create by us, we get a reference
809 * so we can blk_unref() unconditionally */
810 blk_ref(blkdev
->blk
);
812 blk_attach_dev_legacy(blkdev
->blk
, blkdev
);
813 blkdev
->file_size
= blk_getlength(blkdev
->blk
);
814 if (blkdev
->file_size
< 0) {
815 BlockDriverState
*bs
= blk_bs(blkdev
->blk
);
816 const char *drv_name
= bs
? bdrv_get_format_name(bs
) : NULL
;
817 xen_pv_printf(xendev
, 1, "blk_getlength: %d (%s) | drv %s\n",
818 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
820 blkdev
->file_size
= 0;
823 xen_pv_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
824 " size %" PRId64
" (%" PRId64
" MB)\n",
825 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
826 blkdev
->file_size
, blkdev
->file_size
/ MiB
);
828 /* Fill in number of sector size and number of sectors */
829 xenstore_write_be_int(xendev
, "sector-size", blkdev
->file_blk
);
830 xenstore_write_be_int64(xendev
, "sectors",
831 blkdev
->file_size
/ blkdev
->file_blk
);
833 if (xenstore_read_fe_int(xendev
, "ring-page-order",
835 blkdev
->nr_ring_ref
= 1;
837 if (xenstore_read_fe_int(xendev
, "ring-ref",
841 blkdev
->ring_ref
[0] = ring_ref
;
843 } else if (order
>= 0 && order
<= MAX_RING_PAGE_ORDER
) {
844 blkdev
->nr_ring_ref
= 1 << order
;
846 for (i
= 0; i
< blkdev
->nr_ring_ref
; i
++) {
849 key
= g_strdup_printf("ring-ref%u", i
);
854 if (xenstore_read_fe_int(xendev
, key
,
859 blkdev
->ring_ref
[i
] = ring_ref
;
864 xen_pv_printf(xendev
, 0, "invalid ring-page-order: %d\n",
869 if (xenstore_read_fe_int(xendev
, "event-channel",
870 &xendev
->remote_port
) == -1) {
874 if (!xendev
->protocol
) {
875 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
876 } else if (strcmp(xendev
->protocol
, XEN_IO_PROTO_ABI_NATIVE
) == 0) {
877 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
878 } else if (strcmp(xendev
->protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
879 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
880 } else if (strcmp(xendev
->protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
881 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
883 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
886 ring_size
= XC_PAGE_SIZE
* blkdev
->nr_ring_ref
;
887 switch (blkdev
->protocol
) {
888 case BLKIF_PROTOCOL_NATIVE
:
890 blkdev
->max_requests
= __CONST_RING_SIZE(blkif
, ring_size
);
893 case BLKIF_PROTOCOL_X86_32
:
895 blkdev
->max_requests
= __CONST_RING_SIZE(blkif_x86_32
, ring_size
);
898 case BLKIF_PROTOCOL_X86_64
:
900 blkdev
->max_requests
= __CONST_RING_SIZE(blkif_x86_64
, ring_size
);
907 /* Add on the number needed for the ring pages */
908 max_grants
= blkdev
->nr_ring_ref
;
910 xen_be_set_max_grant_refs(xendev
, max_grants
);
911 blkdev
->sring
= xen_be_map_grant_refs(xendev
, blkdev
->ring_ref
,
913 PROT_READ
| PROT_WRITE
);
914 if (!blkdev
->sring
) {
918 switch (blkdev
->protocol
) {
919 case BLKIF_PROTOCOL_NATIVE
:
921 blkif_sring_t
*sring_native
= blkdev
->sring
;
922 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, ring_size
);
925 case BLKIF_PROTOCOL_X86_32
:
927 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
929 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, ring_size
);
932 case BLKIF_PROTOCOL_X86_64
:
934 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
936 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, ring_size
);
941 blk_set_aio_context(blkdev
->blk
, blkdev
->ctx
);
943 xen_be_bind_evtchn(xendev
);
945 xen_pv_printf(xendev
, 1, "ok: proto %s, nr-ring-ref %u, "
946 "remote port %d, local port %d\n",
947 xendev
->protocol
, blkdev
->nr_ring_ref
,
948 xendev
->remote_port
, xendev
->local_port
);
952 static void blk_disconnect(struct XenLegacyDevice
*xendev
)
954 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
956 trace_xen_disk_disconnect(xendev
->name
);
958 aio_context_acquire(blkdev
->ctx
);
961 blk_set_aio_context(blkdev
->blk
, qemu_get_aio_context());
962 blk_detach_dev(blkdev
->blk
, blkdev
);
963 blk_unref(blkdev
->blk
);
966 xen_pv_unbind_evtchn(xendev
);
968 aio_context_release(blkdev
->ctx
);
971 xen_be_unmap_grant_refs(xendev
, blkdev
->sring
,
972 blkdev
->nr_ring_ref
);
973 blkdev
->sring
= NULL
;
977 static int blk_free(struct XenLegacyDevice
*xendev
)
979 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
982 trace_xen_disk_free(xendev
->name
);
984 blk_disconnect(xendev
);
986 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
987 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
988 QLIST_REMOVE(ioreq
, list
);
989 qemu_iovec_destroy(&ioreq
->v
);
993 g_free(blkdev
->params
);
994 g_free(blkdev
->mode
);
995 g_free(blkdev
->type
);
997 g_free(blkdev
->devtype
);
998 qemu_bh_delete(blkdev
->bh
);
999 iothread_destroy(blkdev
->iothread
);
1003 static void blk_event(struct XenLegacyDevice
*xendev
)
1005 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
1007 qemu_bh_schedule(blkdev
->bh
);
1010 struct XenDevOps xen_blkdev_ops
= {
1011 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
1012 .size
= sizeof(struct XenBlkDev
),
1015 .initialise
= blk_connect
,
1016 .disconnect
= blk_disconnect
,