]> git.proxmox.com Git - qemu.git/blame - hw/xen_disk.c
Merge remote-tracking branch 'stefanha/trivial-patches' into staging
[qemu.git] / hw / xen_disk.c
CommitLineData
62d23efa
AL
1/*
2 * xen paravirt block device backend
3 *
4 * (c) Gerd Hoffmann <kraxel@redhat.com>
5 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License along
8167ee88 16 * with this program; if not, see <http://www.gnu.org/licenses/>.
6b620ca3
PB
17 *
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.
62d23efa
AL
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <stdarg.h>
25#include <string.h>
26#include <unistd.h>
27#include <signal.h>
28#include <inttypes.h>
29#include <time.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <sys/ioctl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/mman.h>
36#include <sys/uio.h>
37
38#include <xs.h>
39#include <xenctrl.h>
40#include <xen/io/xenbus.h>
41
42#include "hw.h"
43#include "block_int.h"
44#include "qemu-char.h"
45#include "xen_blkif.h"
46#include "xen_backend.h"
2446333c 47#include "blockdev.h"
62d23efa
AL
48
49/* ------------------------------------------------------------- */
50
51static int syncwrite = 0;
52static int batch_maps = 0;
53
54static int max_requests = 32;
62d23efa
AL
55
56/* ------------------------------------------------------------- */
57
58#define BLOCK_SIZE 512
59#define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
60
61struct ioreq {
62 blkif_request_t req;
63 int16_t status;
64
65 /* parsed request */
66 off_t start;
67 QEMUIOVector v;
68 int presync;
69 int postsync;
70
71 /* grant mapping */
72 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
73 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
74 int prot;
75 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
76 void *pages;
77
78 /* aio status */
79 int aio_inflight;
80 int aio_errors;
81
82 struct XenBlkDev *blkdev;
72cf2d4f 83 QLIST_ENTRY(ioreq) list;
a597e79c 84 BlockAcctCookie acct;
62d23efa
AL
85};
86
87struct XenBlkDev {
88 struct XenDevice xendev; /* must be first */
89 char *params;
90 char *mode;
91 char *type;
92 char *dev;
93 char *devtype;
94 const char *fileproto;
95 const char *filename;
96 int ring_ref;
97 void *sring;
98 int64_t file_blk;
99 int64_t file_size;
100 int protocol;
101 blkif_back_rings_t rings;
102 int more_work;
103 int cnt_map;
104
105 /* request lists */
72cf2d4f
BS
106 QLIST_HEAD(inflight_head, ioreq) inflight;
107 QLIST_HEAD(finished_head, ioreq) finished;
108 QLIST_HEAD(freelist_head, ioreq) freelist;
62d23efa
AL
109 int requests_total;
110 int requests_inflight;
111 int requests_finished;
112
113 /* qemu block driver */
751c6a17 114 DriveInfo *dinfo;
62d23efa
AL
115 BlockDriverState *bs;
116 QEMUBH *bh;
117};
118
119/* ------------------------------------------------------------- */
120
121static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
122{
123 struct ioreq *ioreq = NULL;
124
72cf2d4f 125 if (QLIST_EMPTY(&blkdev->freelist)) {
209cd7ab
AP
126 if (blkdev->requests_total >= max_requests) {
127 goto out;
128 }
129 /* allocate new struct */
7267c094 130 ioreq = g_malloc0(sizeof(*ioreq));
209cd7ab
AP
131 ioreq->blkdev = blkdev;
132 blkdev->requests_total++;
62d23efa
AL
133 qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
134 } else {
209cd7ab
AP
135 /* get one from freelist */
136 ioreq = QLIST_FIRST(&blkdev->freelist);
137 QLIST_REMOVE(ioreq, list);
62d23efa
AL
138 qemu_iovec_reset(&ioreq->v);
139 }
72cf2d4f 140 QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
62d23efa
AL
141 blkdev->requests_inflight++;
142
143out:
144 return ioreq;
145}
146
147static void ioreq_finish(struct ioreq *ioreq)
148{
149 struct XenBlkDev *blkdev = ioreq->blkdev;
150
72cf2d4f
BS
151 QLIST_REMOVE(ioreq, list);
152 QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
62d23efa
AL
153 blkdev->requests_inflight--;
154 blkdev->requests_finished++;
155}
156
157static void ioreq_release(struct ioreq *ioreq)
158{
159 struct XenBlkDev *blkdev = ioreq->blkdev;
160
72cf2d4f 161 QLIST_REMOVE(ioreq, list);
62d23efa
AL
162 memset(ioreq, 0, sizeof(*ioreq));
163 ioreq->blkdev = blkdev;
72cf2d4f 164 QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
62d23efa
AL
165 blkdev->requests_finished--;
166}
167
168/*
169 * translate request into iovec + start offset
170 * do sanity checks along the way
171 */
172static int ioreq_parse(struct ioreq *ioreq)
173{
174 struct XenBlkDev *blkdev = ioreq->blkdev;
175 uintptr_t mem;
176 size_t len;
177 int i;
178
179 xen_be_printf(&blkdev->xendev, 3,
209cd7ab
AP
180 "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
181 ioreq->req.operation, ioreq->req.nr_segments,
182 ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
62d23efa
AL
183 switch (ioreq->req.operation) {
184 case BLKIF_OP_READ:
209cd7ab
AP
185 ioreq->prot = PROT_WRITE; /* to memory */
186 break;
62d23efa 187 case BLKIF_OP_WRITE_BARRIER:
5cbdebe3
SS
188 if (!ioreq->req.nr_segments) {
189 ioreq->presync = 1;
190 return 0;
191 }
209cd7ab
AP
192 if (!syncwrite) {
193 ioreq->presync = ioreq->postsync = 1;
194 }
195 /* fall through */
62d23efa 196 case BLKIF_OP_WRITE:
209cd7ab
AP
197 ioreq->prot = PROT_READ; /* from memory */
198 if (syncwrite) {
199 ioreq->postsync = 1;
200 }
201 break;
62d23efa 202 default:
209cd7ab
AP
203 xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
204 ioreq->req.operation);
205 goto err;
62d23efa
AL
206 };
207
908c7b9f
GH
208 if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') {
209 xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n");
210 goto err;
211 }
212
62d23efa
AL
213 ioreq->start = ioreq->req.sector_number * blkdev->file_blk;
214 for (i = 0; i < ioreq->req.nr_segments; i++) {
209cd7ab
AP
215 if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
216 xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n");
217 goto err;
218 }
219 if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) {
220 xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n");
221 goto err;
222 }
223 if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
224 xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
225 goto err;
226 }
227
228 ioreq->domids[i] = blkdev->xendev.dom;
229 ioreq->refs[i] = ioreq->req.seg[i].gref;
230
231 mem = ioreq->req.seg[i].first_sect * blkdev->file_blk;
232 len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk;
62d23efa
AL
233 qemu_iovec_add(&ioreq->v, (void*)mem, len);
234 }
235 if (ioreq->start + ioreq->v.size > blkdev->file_size) {
209cd7ab
AP
236 xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
237 goto err;
62d23efa
AL
238 }
239 return 0;
240
241err:
242 ioreq->status = BLKIF_RSP_ERROR;
243 return -1;
244}
245
246static void ioreq_unmap(struct ioreq *ioreq)
247{
d5b93ddf 248 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
62d23efa
AL
249 int i;
250
209cd7ab 251 if (ioreq->v.niov == 0) {
62d23efa 252 return;
209cd7ab 253 }
62d23efa 254 if (batch_maps) {
209cd7ab
AP
255 if (!ioreq->pages) {
256 return;
257 }
258 if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->v.niov) != 0) {
259 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
260 strerror(errno));
261 }
262 ioreq->blkdev->cnt_map -= ioreq->v.niov;
263 ioreq->pages = NULL;
62d23efa 264 } else {
209cd7ab
AP
265 for (i = 0; i < ioreq->v.niov; i++) {
266 if (!ioreq->page[i]) {
267 continue;
268 }
269 if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
270 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
271 strerror(errno));
272 }
273 ioreq->blkdev->cnt_map--;
274 ioreq->page[i] = NULL;
275 }
62d23efa
AL
276 }
277}
278
279static int ioreq_map(struct ioreq *ioreq)
280{
d5b93ddf 281 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
62d23efa
AL
282 int i;
283
209cd7ab 284 if (ioreq->v.niov == 0) {
62d23efa 285 return 0;
209cd7ab 286 }
62d23efa 287 if (batch_maps) {
209cd7ab
AP
288 ioreq->pages = xc_gnttab_map_grant_refs
289 (gnt, ioreq->v.niov, ioreq->domids, ioreq->refs, ioreq->prot);
290 if (ioreq->pages == NULL) {
291 xen_be_printf(&ioreq->blkdev->xendev, 0,
292 "can't map %d grant refs (%s, %d maps)\n",
293 ioreq->v.niov, strerror(errno), ioreq->blkdev->cnt_map);
294 return -1;
295 }
296 for (i = 0; i < ioreq->v.niov; i++) {
297 ioreq->v.iov[i].iov_base = ioreq->pages + i * XC_PAGE_SIZE +
298 (uintptr_t)ioreq->v.iov[i].iov_base;
299 }
300 ioreq->blkdev->cnt_map += ioreq->v.niov;
62d23efa 301 } else {
209cd7ab
AP
302 for (i = 0; i < ioreq->v.niov; i++) {
303 ioreq->page[i] = xc_gnttab_map_grant_ref
304 (gnt, ioreq->domids[i], ioreq->refs[i], ioreq->prot);
305 if (ioreq->page[i] == NULL) {
306 xen_be_printf(&ioreq->blkdev->xendev, 0,
307 "can't map grant ref %d (%s, %d maps)\n",
308 ioreq->refs[i], strerror(errno), ioreq->blkdev->cnt_map);
309 ioreq_unmap(ioreq);
310 return -1;
311 }
312 ioreq->v.iov[i].iov_base = ioreq->page[i] + (uintptr_t)ioreq->v.iov[i].iov_base;
313 ioreq->blkdev->cnt_map++;
314 }
62d23efa
AL
315 }
316 return 0;
317}
318
62d23efa
AL
319static void qemu_aio_complete(void *opaque, int ret)
320{
321 struct ioreq *ioreq = opaque;
322
323 if (ret != 0) {
324 xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
325 ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
326 ioreq->aio_errors++;
327 }
328
329 ioreq->aio_inflight--;
209cd7ab 330 if (ioreq->aio_inflight > 0) {
62d23efa 331 return;
209cd7ab 332 }
d56de074
SS
333 if (ioreq->postsync) {
334 bdrv_flush(ioreq->blkdev->bs);
335 }
62d23efa
AL
336
337 ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
338 ioreq_unmap(ioreq);
339 ioreq_finish(ioreq);
a597e79c 340 bdrv_acct_done(ioreq->blkdev->bs, &ioreq->acct);
62d23efa
AL
341 qemu_bh_schedule(ioreq->blkdev->bh);
342}
343
344static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
345{
346 struct XenBlkDev *blkdev = ioreq->blkdev;
347
209cd7ab
AP
348 if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) {
349 goto err_no_map;
350 }
62d23efa
AL
351
352 ioreq->aio_inflight++;
209cd7ab
AP
353 if (ioreq->presync) {
354 bdrv_flush(blkdev->bs); /* FIXME: aio_flush() ??? */
355 }
62d23efa
AL
356
357 switch (ioreq->req.operation) {
358 case BLKIF_OP_READ:
a597e79c 359 bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_READ);
62d23efa
AL
360 ioreq->aio_inflight++;
361 bdrv_aio_readv(blkdev->bs, ioreq->start / BLOCK_SIZE,
362 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
363 qemu_aio_complete, ioreq);
209cd7ab 364 break;
62d23efa
AL
365 case BLKIF_OP_WRITE:
366 case BLKIF_OP_WRITE_BARRIER:
209cd7ab 367 if (!ioreq->req.nr_segments) {
5cbdebe3 368 break;
209cd7ab 369 }
a597e79c
CH
370
371 bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_WRITE);
209bef3e 372 ioreq->aio_inflight++;
62d23efa
AL
373 bdrv_aio_writev(blkdev->bs, ioreq->start / BLOCK_SIZE,
374 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
375 qemu_aio_complete, ioreq);
209cd7ab 376 break;
62d23efa 377 default:
209cd7ab
AP
378 /* unknown operation (shouldn't happen -- parse catches this) */
379 goto err;
62d23efa
AL
380 }
381
62d23efa
AL
382 qemu_aio_complete(ioreq, 0);
383
384 return 0;
385
386err:
f6ec953c
FZ
387 ioreq_unmap(ioreq);
388err_no_map:
389 ioreq_finish(ioreq);
62d23efa
AL
390 ioreq->status = BLKIF_RSP_ERROR;
391 return -1;
392}
393
394static int blk_send_response_one(struct ioreq *ioreq)
395{
396 struct XenBlkDev *blkdev = ioreq->blkdev;
397 int send_notify = 0;
398 int have_requests = 0;
399 blkif_response_t resp;
400 void *dst;
401
402 resp.id = ioreq->req.id;
403 resp.operation = ioreq->req.operation;
404 resp.status = ioreq->status;
405
406 /* Place on the response ring for the relevant domain. */
407 switch (blkdev->protocol) {
408 case BLKIF_PROTOCOL_NATIVE:
209cd7ab
AP
409 dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
410 break;
62d23efa 411 case BLKIF_PROTOCOL_X86_32:
6fcfeff9
BS
412 dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
413 blkdev->rings.x86_32_part.rsp_prod_pvt);
209cd7ab 414 break;
62d23efa 415 case BLKIF_PROTOCOL_X86_64:
6fcfeff9
BS
416 dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
417 blkdev->rings.x86_64_part.rsp_prod_pvt);
209cd7ab 418 break;
62d23efa 419 default:
209cd7ab 420 dst = NULL;
62d23efa
AL
421 }
422 memcpy(dst, &resp, sizeof(resp));
423 blkdev->rings.common.rsp_prod_pvt++;
424
425 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
426 if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
209cd7ab
AP
427 /*
428 * Tail check for pending requests. Allows frontend to avoid
429 * notifications if requests are already in flight (lower
430 * overheads and promotes batching).
431 */
432 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
62d23efa 433 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
209cd7ab 434 have_requests = 1;
62d23efa
AL
435 }
436
209cd7ab
AP
437 if (have_requests) {
438 blkdev->more_work++;
439 }
62d23efa
AL
440 return send_notify;
441}
442
443/* walk finished list, send outstanding responses, free requests */
444static void blk_send_response_all(struct XenBlkDev *blkdev)
445{
446 struct ioreq *ioreq;
447 int send_notify = 0;
448
72cf2d4f
BS
449 while (!QLIST_EMPTY(&blkdev->finished)) {
450 ioreq = QLIST_FIRST(&blkdev->finished);
209cd7ab
AP
451 send_notify += blk_send_response_one(ioreq);
452 ioreq_release(ioreq);
453 }
454 if (send_notify) {
455 xen_be_send_notify(&blkdev->xendev);
62d23efa 456 }
62d23efa
AL
457}
458
459static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
460{
461 switch (blkdev->protocol) {
462 case BLKIF_PROTOCOL_NATIVE:
209cd7ab
AP
463 memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
464 sizeof(ioreq->req));
465 break;
62d23efa 466 case BLKIF_PROTOCOL_X86_32:
6fcfeff9
BS
467 blkif_get_x86_32_req(&ioreq->req,
468 RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
209cd7ab 469 break;
62d23efa 470 case BLKIF_PROTOCOL_X86_64:
6fcfeff9
BS
471 blkif_get_x86_64_req(&ioreq->req,
472 RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
209cd7ab 473 break;
62d23efa
AL
474 }
475 return 0;
476}
477
478static void blk_handle_requests(struct XenBlkDev *blkdev)
479{
480 RING_IDX rc, rp;
481 struct ioreq *ioreq;
482
483 blkdev->more_work = 0;
484
485 rc = blkdev->rings.common.req_cons;
486 rp = blkdev->rings.common.sring->req_prod;
487 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
488
4e5b184d 489 blk_send_response_all(blkdev);
fc1f79f7 490 while (rc != rp) {
62d23efa 491 /* pull request from ring */
209cd7ab 492 if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) {
62d23efa 493 break;
209cd7ab 494 }
62d23efa
AL
495 ioreq = ioreq_start(blkdev);
496 if (ioreq == NULL) {
497 blkdev->more_work++;
498 break;
499 }
500 blk_get_request(blkdev, ioreq, rc);
501 blkdev->rings.common.req_cons = ++rc;
502
503 /* parse them */
504 if (ioreq_parse(ioreq) != 0) {
209cd7ab 505 if (blk_send_response_one(ioreq)) {
62d23efa 506 xen_be_send_notify(&blkdev->xendev);
209cd7ab 507 }
62d23efa
AL
508 ioreq_release(ioreq);
509 continue;
510 }
511
4e5b184d 512 ioreq_runio_qemu_aio(ioreq);
209cd7ab 513 }
62d23efa 514
209cd7ab 515 if (blkdev->more_work && blkdev->requests_inflight < max_requests) {
62d23efa 516 qemu_bh_schedule(blkdev->bh);
209cd7ab 517 }
62d23efa
AL
518}
519
520/* ------------------------------------------------------------- */
521
522static void blk_bh(void *opaque)
523{
524 struct XenBlkDev *blkdev = opaque;
525 blk_handle_requests(blkdev);
526}
527
528static void blk_alloc(struct XenDevice *xendev)
529{
530 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
531
72cf2d4f
BS
532 QLIST_INIT(&blkdev->inflight);
533 QLIST_INIT(&blkdev->finished);
534 QLIST_INIT(&blkdev->freelist);
62d23efa 535 blkdev->bh = qemu_bh_new(blk_bh, blkdev);
209cd7ab 536 if (xen_mode != XEN_EMULATE) {
62d23efa 537 batch_maps = 1;
209cd7ab 538 }
62d23efa
AL
539}
540
541static int blk_init(struct XenDevice *xendev)
542{
543 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
5cce43bb 544 int index, qflags, info = 0;
62d23efa
AL
545
546 /* read xenstore entries */
547 if (blkdev->params == NULL) {
5ea3c2b4 548 char *h = NULL;
209cd7ab 549 blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
5ea3c2b4
SS
550 if (blkdev->params != NULL) {
551 h = strchr(blkdev->params, ':');
552 }
209cd7ab
AP
553 if (h != NULL) {
554 blkdev->fileproto = blkdev->params;
555 blkdev->filename = h+1;
556 *h = 0;
557 } else {
558 blkdev->fileproto = "<unset>";
559 blkdev->filename = blkdev->params;
560 }
561 }
7cef3f4f
SS
562 if (!strcmp("aio", blkdev->fileproto)) {
563 blkdev->fileproto = "raw";
564 }
209cd7ab
AP
565 if (blkdev->mode == NULL) {
566 blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
567 }
568 if (blkdev->type == NULL) {
569 blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
570 }
571 if (blkdev->dev == NULL) {
572 blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
573 }
574 if (blkdev->devtype == NULL) {
575 blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
576 }
62d23efa
AL
577
578 /* do we have all we need? */
579 if (blkdev->params == NULL ||
209cd7ab
AP
580 blkdev->mode == NULL ||
581 blkdev->type == NULL ||
582 blkdev->dev == NULL) {
5ea3c2b4 583 goto out_error;
209cd7ab 584 }
62d23efa
AL
585
586 /* read-only ? */
82091410 587 qflags = BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NATIVE_AIO;
62d23efa 588 if (strcmp(blkdev->mode, "w") == 0) {
82091410 589 qflags |= BDRV_O_RDWR;
62d23efa 590 } else {
209cd7ab 591 info |= VDISK_READONLY;
62d23efa
AL
592 }
593
594 /* cdrom ? */
209cd7ab
AP
595 if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) {
596 info |= VDISK_CDROM;
597 }
62d23efa
AL
598
599 /* init qemu block driver */
751c6a17
GH
600 index = (blkdev->xendev.dev - 202 * 256) / 16;
601 blkdev->dinfo = drive_get(IF_XEN, 0, index);
602 if (!blkdev->dinfo) {
62d23efa
AL
603 /* setup via xenbus -> create new block driver instance */
604 xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
ad717139 605 blkdev->bs = bdrv_new(blkdev->dev);
5ea3c2b4
SS
606 if (blkdev->bs) {
607 if (bdrv_open(blkdev->bs, blkdev->filename, qflags,
608 bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) {
609 bdrv_delete(blkdev->bs);
610 blkdev->bs = NULL;
611 }
612 }
613 if (!blkdev->bs) {
614 goto out_error;
ad717139 615 }
62d23efa
AL
616 } else {
617 /* setup via qemu cmdline -> already setup for us */
618 xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
209cd7ab 619 blkdev->bs = blkdev->dinfo->bdrv;
62d23efa 620 }
fa879d62 621 bdrv_attach_dev_nofail(blkdev->bs, blkdev);
62d23efa
AL
622 blkdev->file_blk = BLOCK_SIZE;
623 blkdev->file_size = bdrv_getlength(blkdev->bs);
624 if (blkdev->file_size < 0) {
625 xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
626 (int)blkdev->file_size, strerror(-blkdev->file_size),
627 blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
209cd7ab 628 blkdev->file_size = 0;
62d23efa 629 }
62d23efa
AL
630
631 xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
209cd7ab
AP
632 " size %" PRId64 " (%" PRId64 " MB)\n",
633 blkdev->type, blkdev->fileproto, blkdev->filename,
634 blkdev->file_size, blkdev->file_size >> 20);
62d23efa
AL
635
636 /* fill info */
5cce43bb 637 xenstore_write_be_int(&blkdev->xendev, "feature-barrier", 1);
62d23efa
AL
638 xenstore_write_be_int(&blkdev->xendev, "info", info);
639 xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
640 xenstore_write_be_int(&blkdev->xendev, "sectors",
209cd7ab 641 blkdev->file_size / blkdev->file_blk);
62d23efa 642 return 0;
5ea3c2b4
SS
643
644out_error:
7267c094 645 g_free(blkdev->params);
5ea3c2b4 646 blkdev->params = NULL;
7267c094 647 g_free(blkdev->mode);
5ea3c2b4 648 blkdev->mode = NULL;
7267c094 649 g_free(blkdev->type);
5ea3c2b4 650 blkdev->type = NULL;
7267c094 651 g_free(blkdev->dev);
5ea3c2b4 652 blkdev->dev = NULL;
7267c094 653 g_free(blkdev->devtype);
5ea3c2b4
SS
654 blkdev->devtype = NULL;
655 return -1;
62d23efa
AL
656}
657
658static int blk_connect(struct XenDevice *xendev)
659{
660 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
661
209cd7ab
AP
662 if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) {
663 return -1;
664 }
62d23efa 665 if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
209cd7ab
AP
666 &blkdev->xendev.remote_port) == -1) {
667 return -1;
668 }
62d23efa
AL
669
670 blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
671 if (blkdev->xendev.protocol) {
209cd7ab 672 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
62d23efa 673 blkdev->protocol = BLKIF_PROTOCOL_X86_32;
209cd7ab
AP
674 }
675 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
62d23efa 676 blkdev->protocol = BLKIF_PROTOCOL_X86_64;
209cd7ab 677 }
62d23efa
AL
678 }
679
680 blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
209cd7ab
AP
681 blkdev->xendev.dom,
682 blkdev->ring_ref,
683 PROT_READ | PROT_WRITE);
684 if (!blkdev->sring) {
685 return -1;
686 }
62d23efa
AL
687 blkdev->cnt_map++;
688
689 switch (blkdev->protocol) {
690 case BLKIF_PROTOCOL_NATIVE:
691 {
209cd7ab
AP
692 blkif_sring_t *sring_native = blkdev->sring;
693 BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
694 break;
62d23efa
AL
695 }
696 case BLKIF_PROTOCOL_X86_32:
697 {
209cd7ab 698 blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
6fcfeff9
BS
699
700 BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
209cd7ab 701 break;
62d23efa
AL
702 }
703 case BLKIF_PROTOCOL_X86_64:
704 {
209cd7ab 705 blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
6fcfeff9
BS
706
707 BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
209cd7ab 708 break;
62d23efa
AL
709 }
710 }
711
712 xen_be_bind_evtchn(&blkdev->xendev);
713
714 xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
209cd7ab
AP
715 "remote port %d, local port %d\n",
716 blkdev->xendev.protocol, blkdev->ring_ref,
717 blkdev->xendev.remote_port, blkdev->xendev.local_port);
62d23efa
AL
718 return 0;
719}
720
721static void blk_disconnect(struct XenDevice *xendev)
722{
723 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
724
725 if (blkdev->bs) {
751c6a17 726 if (!blkdev->dinfo) {
62d23efa
AL
727 /* close/delete only if we created it ourself */
728 bdrv_close(blkdev->bs);
7429f2e1 729 bdrv_detach_dev(blkdev->bs, blkdev);
62d23efa
AL
730 bdrv_delete(blkdev->bs);
731 }
209cd7ab 732 blkdev->bs = NULL;
62d23efa
AL
733 }
734 xen_be_unbind_evtchn(&blkdev->xendev);
735
736 if (blkdev->sring) {
209cd7ab
AP
737 xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
738 blkdev->cnt_map--;
739 blkdev->sring = NULL;
62d23efa
AL
740 }
741}
742
743static int blk_free(struct XenDevice *xendev)
744{
745 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
746 struct ioreq *ioreq;
747
77ba8fef
SS
748 if (blkdev->bs || blkdev->sring) {
749 blk_disconnect(xendev);
750 }
751
72cf2d4f 752 while (!QLIST_EMPTY(&blkdev->freelist)) {
209cd7ab 753 ioreq = QLIST_FIRST(&blkdev->freelist);
72cf2d4f 754 QLIST_REMOVE(ioreq, list);
62d23efa 755 qemu_iovec_destroy(&ioreq->v);
7267c094 756 g_free(ioreq);
62d23efa
AL
757 }
758
7267c094
AL
759 g_free(blkdev->params);
760 g_free(blkdev->mode);
761 g_free(blkdev->type);
762 g_free(blkdev->dev);
763 g_free(blkdev->devtype);
62d23efa
AL
764 qemu_bh_delete(blkdev->bh);
765 return 0;
766}
767
768static void blk_event(struct XenDevice *xendev)
769{
770 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
771
772 qemu_bh_schedule(blkdev->bh);
773}
774
775struct XenDevOps xen_blkdev_ops = {
776 .size = sizeof(struct XenBlkDev),
777 .flags = DEVOPS_FLAG_NEED_GNTDEV,
778 .alloc = blk_alloc,
779 .init = blk_init,
384087b2 780 .initialise = blk_connect,
62d23efa
AL
781 .disconnect = blk_disconnect,
782 .event = blk_event,
783 .free = blk_free,
784};