]> git.proxmox.com Git - mirror_qemu.git/blob - hw/block/xen_disk.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-vnc-20151116-1' into staging
[mirror_qemu.git] / hw / block / xen_disk.c
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
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
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.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/uio.h>
36
37 #include "hw/hw.h"
38 #include "hw/xen/xen_backend.h"
39 #include "xen_blkif.h"
40 #include "sysemu/blockdev.h"
41 #include "sysemu/block-backend.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qstring.h"
44
45 /* ------------------------------------------------------------- */
46
47 static int batch_maps = 0;
48
49 static int max_requests = 32;
50
51 /* ------------------------------------------------------------- */
52
53 #define BLOCK_SIZE 512
54 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
55
56 struct PersistentGrant {
57 void *page;
58 struct XenBlkDev *blkdev;
59 };
60
61 typedef struct PersistentGrant PersistentGrant;
62
63 struct PersistentRegion {
64 void *addr;
65 int num;
66 };
67
68 typedef struct PersistentRegion PersistentRegion;
69
70 struct ioreq {
71 blkif_request_t req;
72 int16_t status;
73
74 /* parsed request */
75 off_t start;
76 QEMUIOVector v;
77 int presync;
78 int postsync;
79 uint8_t mapped;
80
81 /* grant mapping */
82 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
83 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
84 int prot;
85 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
86 void *pages;
87 int num_unmap;
88
89 /* aio status */
90 int aio_inflight;
91 int aio_errors;
92
93 struct XenBlkDev *blkdev;
94 QLIST_ENTRY(ioreq) list;
95 BlockAcctCookie acct;
96 };
97
98 struct XenBlkDev {
99 struct XenDevice xendev; /* must be first */
100 char *params;
101 char *mode;
102 char *type;
103 char *dev;
104 char *devtype;
105 bool directiosafe;
106 const char *fileproto;
107 const char *filename;
108 int ring_ref;
109 void *sring;
110 int64_t file_blk;
111 int64_t file_size;
112 int protocol;
113 blkif_back_rings_t rings;
114 int more_work;
115 int cnt_map;
116
117 /* request lists */
118 QLIST_HEAD(inflight_head, ioreq) inflight;
119 QLIST_HEAD(finished_head, ioreq) finished;
120 QLIST_HEAD(freelist_head, ioreq) freelist;
121 int requests_total;
122 int requests_inflight;
123 int requests_finished;
124
125 /* Persistent grants extension */
126 gboolean feature_discard;
127 gboolean feature_persistent;
128 GTree *persistent_gnts;
129 GSList *persistent_regions;
130 unsigned int persistent_gnt_count;
131 unsigned int max_grants;
132
133 /* qemu block driver */
134 DriveInfo *dinfo;
135 BlockBackend *blk;
136 QEMUBH *bh;
137 };
138
139 /* ------------------------------------------------------------- */
140
141 static void ioreq_reset(struct ioreq *ioreq)
142 {
143 memset(&ioreq->req, 0, sizeof(ioreq->req));
144 ioreq->status = 0;
145 ioreq->start = 0;
146 ioreq->presync = 0;
147 ioreq->postsync = 0;
148 ioreq->mapped = 0;
149
150 memset(ioreq->domids, 0, sizeof(ioreq->domids));
151 memset(ioreq->refs, 0, sizeof(ioreq->refs));
152 ioreq->prot = 0;
153 memset(ioreq->page, 0, sizeof(ioreq->page));
154 ioreq->pages = NULL;
155
156 ioreq->aio_inflight = 0;
157 ioreq->aio_errors = 0;
158
159 ioreq->blkdev = NULL;
160 memset(&ioreq->list, 0, sizeof(ioreq->list));
161 memset(&ioreq->acct, 0, sizeof(ioreq->acct));
162
163 qemu_iovec_reset(&ioreq->v);
164 }
165
166 static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
167 {
168 uint ua = GPOINTER_TO_UINT(a);
169 uint ub = GPOINTER_TO_UINT(b);
170 return (ua > ub) - (ua < ub);
171 }
172
173 static void destroy_grant(gpointer pgnt)
174 {
175 PersistentGrant *grant = pgnt;
176 XenGnttab gnt = grant->blkdev->xendev.gnttabdev;
177
178 if (xc_gnttab_munmap(gnt, grant->page, 1) != 0) {
179 xen_be_printf(&grant->blkdev->xendev, 0,
180 "xc_gnttab_munmap failed: %s\n",
181 strerror(errno));
182 }
183 grant->blkdev->persistent_gnt_count--;
184 xen_be_printf(&grant->blkdev->xendev, 3,
185 "unmapped grant %p\n", grant->page);
186 g_free(grant);
187 }
188
189 static void remove_persistent_region(gpointer data, gpointer dev)
190 {
191 PersistentRegion *region = data;
192 struct XenBlkDev *blkdev = dev;
193 XenGnttab gnt = blkdev->xendev.gnttabdev;
194
195 if (xc_gnttab_munmap(gnt, region->addr, region->num) != 0) {
196 xen_be_printf(&blkdev->xendev, 0,
197 "xc_gnttab_munmap region %p failed: %s\n",
198 region->addr, strerror(errno));
199 }
200 xen_be_printf(&blkdev->xendev, 3,
201 "unmapped grant region %p with %d pages\n",
202 region->addr, region->num);
203 g_free(region);
204 }
205
206 static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
207 {
208 struct ioreq *ioreq = NULL;
209
210 if (QLIST_EMPTY(&blkdev->freelist)) {
211 if (blkdev->requests_total >= max_requests) {
212 goto out;
213 }
214 /* allocate new struct */
215 ioreq = g_malloc0(sizeof(*ioreq));
216 ioreq->blkdev = blkdev;
217 blkdev->requests_total++;
218 qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
219 } else {
220 /* get one from freelist */
221 ioreq = QLIST_FIRST(&blkdev->freelist);
222 QLIST_REMOVE(ioreq, list);
223 }
224 QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
225 blkdev->requests_inflight++;
226
227 out:
228 return ioreq;
229 }
230
231 static void ioreq_finish(struct ioreq *ioreq)
232 {
233 struct XenBlkDev *blkdev = ioreq->blkdev;
234
235 QLIST_REMOVE(ioreq, list);
236 QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
237 blkdev->requests_inflight--;
238 blkdev->requests_finished++;
239 }
240
241 static void ioreq_release(struct ioreq *ioreq, bool finish)
242 {
243 struct XenBlkDev *blkdev = ioreq->blkdev;
244
245 QLIST_REMOVE(ioreq, list);
246 ioreq_reset(ioreq);
247 ioreq->blkdev = blkdev;
248 QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
249 if (finish) {
250 blkdev->requests_finished--;
251 } else {
252 blkdev->requests_inflight--;
253 }
254 }
255
256 /*
257 * translate request into iovec + start offset
258 * do sanity checks along the way
259 */
260 static int ioreq_parse(struct ioreq *ioreq)
261 {
262 struct XenBlkDev *blkdev = ioreq->blkdev;
263 uintptr_t mem;
264 size_t len;
265 int i;
266
267 xen_be_printf(&blkdev->xendev, 3,
268 "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
269 ioreq->req.operation, ioreq->req.nr_segments,
270 ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
271 switch (ioreq->req.operation) {
272 case BLKIF_OP_READ:
273 ioreq->prot = PROT_WRITE; /* to memory */
274 break;
275 case BLKIF_OP_FLUSH_DISKCACHE:
276 ioreq->presync = 1;
277 if (!ioreq->req.nr_segments) {
278 return 0;
279 }
280 /* fall through */
281 case BLKIF_OP_WRITE:
282 ioreq->prot = PROT_READ; /* from memory */
283 break;
284 case BLKIF_OP_DISCARD:
285 return 0;
286 default:
287 xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
288 ioreq->req.operation);
289 goto err;
290 };
291
292 if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') {
293 xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n");
294 goto err;
295 }
296
297 ioreq->start = ioreq->req.sector_number * blkdev->file_blk;
298 for (i = 0; i < ioreq->req.nr_segments; i++) {
299 if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
300 xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n");
301 goto err;
302 }
303 if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) {
304 xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n");
305 goto err;
306 }
307 if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
308 xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
309 goto err;
310 }
311
312 ioreq->domids[i] = blkdev->xendev.dom;
313 ioreq->refs[i] = ioreq->req.seg[i].gref;
314
315 mem = ioreq->req.seg[i].first_sect * blkdev->file_blk;
316 len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk;
317 qemu_iovec_add(&ioreq->v, (void*)mem, len);
318 }
319 if (ioreq->start + ioreq->v.size > blkdev->file_size) {
320 xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
321 goto err;
322 }
323 return 0;
324
325 err:
326 ioreq->status = BLKIF_RSP_ERROR;
327 return -1;
328 }
329
330 static void ioreq_unmap(struct ioreq *ioreq)
331 {
332 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
333 int i;
334
335 if (ioreq->num_unmap == 0 || ioreq->mapped == 0) {
336 return;
337 }
338 if (batch_maps) {
339 if (!ioreq->pages) {
340 return;
341 }
342 if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
343 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
344 strerror(errno));
345 }
346 ioreq->blkdev->cnt_map -= ioreq->num_unmap;
347 ioreq->pages = NULL;
348 } else {
349 for (i = 0; i < ioreq->num_unmap; i++) {
350 if (!ioreq->page[i]) {
351 continue;
352 }
353 if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
354 xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
355 strerror(errno));
356 }
357 ioreq->blkdev->cnt_map--;
358 ioreq->page[i] = NULL;
359 }
360 }
361 ioreq->mapped = 0;
362 }
363
364 static int ioreq_map(struct ioreq *ioreq)
365 {
366 XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
367 uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
368 uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
369 void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
370 int i, j, new_maps = 0;
371 PersistentGrant *grant;
372 PersistentRegion *region;
373 /* domids and refs variables will contain the information necessary
374 * to map the grants that are needed to fulfill this request.
375 *
376 * After mapping the needed grants, the page array will contain the
377 * memory address of each granted page in the order specified in ioreq
378 * (disregarding if it's a persistent grant or not).
379 */
380
381 if (ioreq->v.niov == 0 || ioreq->mapped == 1) {
382 return 0;
383 }
384 if (ioreq->blkdev->feature_persistent) {
385 for (i = 0; i < ioreq->v.niov; i++) {
386 grant = g_tree_lookup(ioreq->blkdev->persistent_gnts,
387 GUINT_TO_POINTER(ioreq->refs[i]));
388
389 if (grant != NULL) {
390 page[i] = grant->page;
391 xen_be_printf(&ioreq->blkdev->xendev, 3,
392 "using persistent-grant %" PRIu32 "\n",
393 ioreq->refs[i]);
394 } else {
395 /* Add the grant to the list of grants that
396 * should be mapped
397 */
398 domids[new_maps] = ioreq->domids[i];
399 refs[new_maps] = ioreq->refs[i];
400 page[i] = NULL;
401 new_maps++;
402 }
403 }
404 /* Set the protection to RW, since grants may be reused later
405 * with a different protection than the one needed for this request
406 */
407 ioreq->prot = PROT_WRITE | PROT_READ;
408 } else {
409 /* All grants in the request should be mapped */
410 memcpy(refs, ioreq->refs, sizeof(refs));
411 memcpy(domids, ioreq->domids, sizeof(domids));
412 memset(page, 0, sizeof(page));
413 new_maps = ioreq->v.niov;
414 }
415
416 if (batch_maps && new_maps) {
417 ioreq->pages = xc_gnttab_map_grant_refs
418 (gnt, new_maps, domids, refs, ioreq->prot);
419 if (ioreq->pages == NULL) {
420 xen_be_printf(&ioreq->blkdev->xendev, 0,
421 "can't map %d grant refs (%s, %d maps)\n",
422 new_maps, strerror(errno), ioreq->blkdev->cnt_map);
423 return -1;
424 }
425 for (i = 0, j = 0; i < ioreq->v.niov; i++) {
426 if (page[i] == NULL) {
427 page[i] = ioreq->pages + (j++) * XC_PAGE_SIZE;
428 }
429 }
430 ioreq->blkdev->cnt_map += new_maps;
431 } else if (new_maps) {
432 for (i = 0; i < new_maps; i++) {
433 ioreq->page[i] = xc_gnttab_map_grant_ref
434 (gnt, domids[i], refs[i], ioreq->prot);
435 if (ioreq->page[i] == NULL) {
436 xen_be_printf(&ioreq->blkdev->xendev, 0,
437 "can't map grant ref %d (%s, %d maps)\n",
438 refs[i], strerror(errno), ioreq->blkdev->cnt_map);
439 ioreq->mapped = 1;
440 ioreq_unmap(ioreq);
441 return -1;
442 }
443 ioreq->blkdev->cnt_map++;
444 }
445 for (i = 0, j = 0; i < ioreq->v.niov; i++) {
446 if (page[i] == NULL) {
447 page[i] = ioreq->page[j++];
448 }
449 }
450 }
451 if (ioreq->blkdev->feature_persistent && new_maps != 0 &&
452 (!batch_maps || (ioreq->blkdev->persistent_gnt_count + new_maps <=
453 ioreq->blkdev->max_grants))) {
454 /*
455 * If we are using persistent grants and batch mappings only
456 * add the new maps to the list of persistent grants if the whole
457 * area can be persistently mapped.
458 */
459 if (batch_maps) {
460 region = g_malloc0(sizeof(*region));
461 region->addr = ioreq->pages;
462 region->num = new_maps;
463 ioreq->blkdev->persistent_regions = g_slist_append(
464 ioreq->blkdev->persistent_regions,
465 region);
466 }
467 while ((ioreq->blkdev->persistent_gnt_count < ioreq->blkdev->max_grants)
468 && new_maps) {
469 /* Go through the list of newly mapped grants and add as many
470 * as possible to the list of persistently mapped grants.
471 *
472 * Since we start at the end of ioreq->page(s), we only need
473 * to decrease new_maps to prevent this granted pages from
474 * being unmapped in ioreq_unmap.
475 */
476 grant = g_malloc0(sizeof(*grant));
477 new_maps--;
478 if (batch_maps) {
479 grant->page = ioreq->pages + (new_maps) * XC_PAGE_SIZE;
480 } else {
481 grant->page = ioreq->page[new_maps];
482 }
483 grant->blkdev = ioreq->blkdev;
484 xen_be_printf(&ioreq->blkdev->xendev, 3,
485 "adding grant %" PRIu32 " page: %p\n",
486 refs[new_maps], grant->page);
487 g_tree_insert(ioreq->blkdev->persistent_gnts,
488 GUINT_TO_POINTER(refs[new_maps]),
489 grant);
490 ioreq->blkdev->persistent_gnt_count++;
491 }
492 assert(!batch_maps || new_maps == 0);
493 }
494 for (i = 0; i < ioreq->v.niov; i++) {
495 ioreq->v.iov[i].iov_base += (uintptr_t)page[i];
496 }
497 ioreq->mapped = 1;
498 ioreq->num_unmap = new_maps;
499 return 0;
500 }
501
502 static int ioreq_runio_qemu_aio(struct ioreq *ioreq);
503
504 static void qemu_aio_complete(void *opaque, int ret)
505 {
506 struct ioreq *ioreq = opaque;
507
508 if (ret != 0) {
509 xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
510 ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
511 ioreq->aio_errors++;
512 }
513
514 ioreq->aio_inflight--;
515 if (ioreq->presync) {
516 ioreq->presync = 0;
517 ioreq_runio_qemu_aio(ioreq);
518 return;
519 }
520 if (ioreq->aio_inflight > 0) {
521 return;
522 }
523 if (ioreq->postsync) {
524 ioreq->postsync = 0;
525 ioreq->aio_inflight++;
526 blk_aio_flush(ioreq->blkdev->blk, qemu_aio_complete, ioreq);
527 return;
528 }
529
530 ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
531 ioreq_unmap(ioreq);
532 ioreq_finish(ioreq);
533 switch (ioreq->req.operation) {
534 case BLKIF_OP_WRITE:
535 case BLKIF_OP_FLUSH_DISKCACHE:
536 if (!ioreq->req.nr_segments) {
537 break;
538 }
539 case BLKIF_OP_READ:
540 if (ioreq->status == BLKIF_RSP_OKAY) {
541 block_acct_done(blk_get_stats(ioreq->blkdev->blk), &ioreq->acct);
542 } else {
543 block_acct_failed(blk_get_stats(ioreq->blkdev->blk), &ioreq->acct);
544 }
545 break;
546 case BLKIF_OP_DISCARD:
547 default:
548 break;
549 }
550 qemu_bh_schedule(ioreq->blkdev->bh);
551 }
552
553 static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
554 {
555 struct XenBlkDev *blkdev = ioreq->blkdev;
556
557 if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) {
558 goto err_no_map;
559 }
560
561 ioreq->aio_inflight++;
562 if (ioreq->presync) {
563 blk_aio_flush(ioreq->blkdev->blk, qemu_aio_complete, ioreq);
564 return 0;
565 }
566
567 switch (ioreq->req.operation) {
568 case BLKIF_OP_READ:
569 block_acct_start(blk_get_stats(blkdev->blk), &ioreq->acct,
570 ioreq->v.size, BLOCK_ACCT_READ);
571 ioreq->aio_inflight++;
572 blk_aio_readv(blkdev->blk, ioreq->start / BLOCK_SIZE,
573 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
574 qemu_aio_complete, ioreq);
575 break;
576 case BLKIF_OP_WRITE:
577 case BLKIF_OP_FLUSH_DISKCACHE:
578 if (!ioreq->req.nr_segments) {
579 break;
580 }
581
582 block_acct_start(blk_get_stats(blkdev->blk), &ioreq->acct,
583 ioreq->v.size,
584 ioreq->req.operation == BLKIF_OP_WRITE ?
585 BLOCK_ACCT_WRITE : BLOCK_ACCT_FLUSH);
586 ioreq->aio_inflight++;
587 blk_aio_writev(blkdev->blk, ioreq->start / BLOCK_SIZE,
588 &ioreq->v, ioreq->v.size / BLOCK_SIZE,
589 qemu_aio_complete, ioreq);
590 break;
591 case BLKIF_OP_DISCARD:
592 {
593 struct blkif_request_discard *discard_req = (void *)&ioreq->req;
594 ioreq->aio_inflight++;
595 blk_aio_discard(blkdev->blk,
596 discard_req->sector_number, discard_req->nr_sectors,
597 qemu_aio_complete, ioreq);
598 break;
599 }
600 default:
601 /* unknown operation (shouldn't happen -- parse catches this) */
602 goto err;
603 }
604
605 qemu_aio_complete(ioreq, 0);
606
607 return 0;
608
609 err:
610 ioreq_unmap(ioreq);
611 err_no_map:
612 ioreq_finish(ioreq);
613 ioreq->status = BLKIF_RSP_ERROR;
614 return -1;
615 }
616
617 static int blk_send_response_one(struct ioreq *ioreq)
618 {
619 struct XenBlkDev *blkdev = ioreq->blkdev;
620 int send_notify = 0;
621 int have_requests = 0;
622 blkif_response_t resp;
623 void *dst;
624
625 resp.id = ioreq->req.id;
626 resp.operation = ioreq->req.operation;
627 resp.status = ioreq->status;
628
629 /* Place on the response ring for the relevant domain. */
630 switch (blkdev->protocol) {
631 case BLKIF_PROTOCOL_NATIVE:
632 dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
633 break;
634 case BLKIF_PROTOCOL_X86_32:
635 dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
636 blkdev->rings.x86_32_part.rsp_prod_pvt);
637 break;
638 case BLKIF_PROTOCOL_X86_64:
639 dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
640 blkdev->rings.x86_64_part.rsp_prod_pvt);
641 break;
642 default:
643 dst = NULL;
644 return 0;
645 }
646 memcpy(dst, &resp, sizeof(resp));
647 blkdev->rings.common.rsp_prod_pvt++;
648
649 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
650 if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
651 /*
652 * Tail check for pending requests. Allows frontend to avoid
653 * notifications if requests are already in flight (lower
654 * overheads and promotes batching).
655 */
656 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
657 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
658 have_requests = 1;
659 }
660
661 if (have_requests) {
662 blkdev->more_work++;
663 }
664 return send_notify;
665 }
666
667 /* walk finished list, send outstanding responses, free requests */
668 static void blk_send_response_all(struct XenBlkDev *blkdev)
669 {
670 struct ioreq *ioreq;
671 int send_notify = 0;
672
673 while (!QLIST_EMPTY(&blkdev->finished)) {
674 ioreq = QLIST_FIRST(&blkdev->finished);
675 send_notify += blk_send_response_one(ioreq);
676 ioreq_release(ioreq, true);
677 }
678 if (send_notify) {
679 xen_be_send_notify(&blkdev->xendev);
680 }
681 }
682
683 static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
684 {
685 switch (blkdev->protocol) {
686 case BLKIF_PROTOCOL_NATIVE:
687 memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
688 sizeof(ioreq->req));
689 break;
690 case BLKIF_PROTOCOL_X86_32:
691 blkif_get_x86_32_req(&ioreq->req,
692 RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
693 break;
694 case BLKIF_PROTOCOL_X86_64:
695 blkif_get_x86_64_req(&ioreq->req,
696 RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
697 break;
698 }
699 return 0;
700 }
701
702 static void blk_handle_requests(struct XenBlkDev *blkdev)
703 {
704 RING_IDX rc, rp;
705 struct ioreq *ioreq;
706
707 blkdev->more_work = 0;
708
709 rc = blkdev->rings.common.req_cons;
710 rp = blkdev->rings.common.sring->req_prod;
711 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
712
713 blk_send_response_all(blkdev);
714 while (rc != rp) {
715 /* pull request from ring */
716 if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) {
717 break;
718 }
719 ioreq = ioreq_start(blkdev);
720 if (ioreq == NULL) {
721 blkdev->more_work++;
722 break;
723 }
724 blk_get_request(blkdev, ioreq, rc);
725 blkdev->rings.common.req_cons = ++rc;
726
727 /* parse them */
728 if (ioreq_parse(ioreq) != 0) {
729
730 switch (ioreq->req.operation) {
731 case BLKIF_OP_READ:
732 block_acct_invalid(blk_get_stats(blkdev->blk),
733 BLOCK_ACCT_READ);
734 break;
735 case BLKIF_OP_WRITE:
736 block_acct_invalid(blk_get_stats(blkdev->blk),
737 BLOCK_ACCT_WRITE);
738 break;
739 case BLKIF_OP_FLUSH_DISKCACHE:
740 block_acct_invalid(blk_get_stats(blkdev->blk),
741 BLOCK_ACCT_FLUSH);
742 default:
743 break;
744 };
745
746 if (blk_send_response_one(ioreq)) {
747 xen_be_send_notify(&blkdev->xendev);
748 }
749 ioreq_release(ioreq, false);
750 continue;
751 }
752
753 ioreq_runio_qemu_aio(ioreq);
754 }
755
756 if (blkdev->more_work && blkdev->requests_inflight < max_requests) {
757 qemu_bh_schedule(blkdev->bh);
758 }
759 }
760
761 /* ------------------------------------------------------------- */
762
763 static void blk_bh(void *opaque)
764 {
765 struct XenBlkDev *blkdev = opaque;
766 blk_handle_requests(blkdev);
767 }
768
769 /*
770 * We need to account for the grant allocations requiring contiguous
771 * chunks; the worst case number would be
772 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
773 * but in order to keep things simple just use
774 * 2 * max_req * max_seg.
775 */
776 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
777
778 static void blk_alloc(struct XenDevice *xendev)
779 {
780 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
781
782 QLIST_INIT(&blkdev->inflight);
783 QLIST_INIT(&blkdev->finished);
784 QLIST_INIT(&blkdev->freelist);
785 blkdev->bh = qemu_bh_new(blk_bh, blkdev);
786 if (xen_mode != XEN_EMULATE) {
787 batch_maps = 1;
788 }
789 if (xc_gnttab_set_max_grants(xendev->gnttabdev,
790 MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
791 xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
792 strerror(errno));
793 }
794 }
795
796 static void blk_parse_discard(struct XenBlkDev *blkdev)
797 {
798 int enable;
799
800 blkdev->feature_discard = true;
801
802 if (xenstore_read_be_int(&blkdev->xendev, "discard-enable", &enable) == 0) {
803 blkdev->feature_discard = !!enable;
804 }
805
806 if (blkdev->feature_discard) {
807 xenstore_write_be_int(&blkdev->xendev, "feature-discard", 1);
808 }
809 }
810
811 static int blk_init(struct XenDevice *xendev)
812 {
813 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
814 int info = 0;
815 char *directiosafe = NULL;
816
817 /* read xenstore entries */
818 if (blkdev->params == NULL) {
819 char *h = NULL;
820 blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
821 if (blkdev->params != NULL) {
822 h = strchr(blkdev->params, ':');
823 }
824 if (h != NULL) {
825 blkdev->fileproto = blkdev->params;
826 blkdev->filename = h+1;
827 *h = 0;
828 } else {
829 blkdev->fileproto = "<unset>";
830 blkdev->filename = blkdev->params;
831 }
832 }
833 if (!strcmp("aio", blkdev->fileproto)) {
834 blkdev->fileproto = "raw";
835 }
836 if (blkdev->mode == NULL) {
837 blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
838 }
839 if (blkdev->type == NULL) {
840 blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
841 }
842 if (blkdev->dev == NULL) {
843 blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
844 }
845 if (blkdev->devtype == NULL) {
846 blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
847 }
848 directiosafe = xenstore_read_be_str(&blkdev->xendev, "direct-io-safe");
849 blkdev->directiosafe = (directiosafe && atoi(directiosafe));
850
851 /* do we have all we need? */
852 if (blkdev->params == NULL ||
853 blkdev->mode == NULL ||
854 blkdev->type == NULL ||
855 blkdev->dev == NULL) {
856 goto out_error;
857 }
858
859 /* read-only ? */
860 if (strcmp(blkdev->mode, "w")) {
861 info |= VDISK_READONLY;
862 }
863
864 /* cdrom ? */
865 if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) {
866 info |= VDISK_CDROM;
867 }
868
869 blkdev->file_blk = BLOCK_SIZE;
870
871 /* fill info
872 * blk_connect supplies sector-size and sectors
873 */
874 xenstore_write_be_int(&blkdev->xendev, "feature-flush-cache", 1);
875 xenstore_write_be_int(&blkdev->xendev, "feature-persistent", 1);
876 xenstore_write_be_int(&blkdev->xendev, "info", info);
877
878 blk_parse_discard(blkdev);
879
880 g_free(directiosafe);
881 return 0;
882
883 out_error:
884 g_free(blkdev->params);
885 blkdev->params = NULL;
886 g_free(blkdev->mode);
887 blkdev->mode = NULL;
888 g_free(blkdev->type);
889 blkdev->type = NULL;
890 g_free(blkdev->dev);
891 blkdev->dev = NULL;
892 g_free(blkdev->devtype);
893 blkdev->devtype = NULL;
894 g_free(directiosafe);
895 blkdev->directiosafe = false;
896 return -1;
897 }
898
899 static int blk_connect(struct XenDevice *xendev)
900 {
901 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
902 int pers, index, qflags;
903 bool readonly = true;
904
905 /* read-only ? */
906 if (blkdev->directiosafe) {
907 qflags = BDRV_O_NOCACHE | BDRV_O_NATIVE_AIO;
908 } else {
909 qflags = BDRV_O_CACHE_WB;
910 }
911 if (strcmp(blkdev->mode, "w") == 0) {
912 qflags |= BDRV_O_RDWR;
913 readonly = false;
914 }
915 if (blkdev->feature_discard) {
916 qflags |= BDRV_O_UNMAP;
917 }
918
919 /* init qemu block driver */
920 index = (blkdev->xendev.dev - 202 * 256) / 16;
921 blkdev->dinfo = drive_get(IF_XEN, 0, index);
922 if (!blkdev->dinfo) {
923 Error *local_err = NULL;
924 QDict *options = NULL;
925
926 if (strcmp(blkdev->fileproto, "<unset>")) {
927 options = qdict_new();
928 qdict_put(options, "driver", qstring_from_str(blkdev->fileproto));
929 }
930
931 /* setup via xenbus -> create new block driver instance */
932 xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
933 blkdev->blk = blk_new_open(blkdev->dev, blkdev->filename, NULL, options,
934 qflags, &local_err);
935 if (!blkdev->blk) {
936 xen_be_printf(&blkdev->xendev, 0, "error: %s\n",
937 error_get_pretty(local_err));
938 error_free(local_err);
939 return -1;
940 }
941 } else {
942 /* setup via qemu cmdline -> already setup for us */
943 xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
944 blkdev->blk = blk_by_legacy_dinfo(blkdev->dinfo);
945 if (blk_is_read_only(blkdev->blk) && !readonly) {
946 xen_be_printf(&blkdev->xendev, 0, "Unexpected read-only drive");
947 blkdev->blk = NULL;
948 return -1;
949 }
950 /* blkdev->blk is not create by us, we get a reference
951 * so we can blk_unref() unconditionally */
952 blk_ref(blkdev->blk);
953 }
954 blk_attach_dev_nofail(blkdev->blk, blkdev);
955 blkdev->file_size = blk_getlength(blkdev->blk);
956 if (blkdev->file_size < 0) {
957 BlockDriverState *bs = blk_bs(blkdev->blk);
958 const char *drv_name = bs ? bdrv_get_format_name(bs) : NULL;
959 xen_be_printf(&blkdev->xendev, 1, "blk_getlength: %d (%s) | drv %s\n",
960 (int)blkdev->file_size, strerror(-blkdev->file_size),
961 drv_name ?: "-");
962 blkdev->file_size = 0;
963 }
964
965 xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
966 " size %" PRId64 " (%" PRId64 " MB)\n",
967 blkdev->type, blkdev->fileproto, blkdev->filename,
968 blkdev->file_size, blkdev->file_size >> 20);
969
970 /* Fill in number of sector size and number of sectors */
971 xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
972 xenstore_write_be_int64(&blkdev->xendev, "sectors",
973 blkdev->file_size / blkdev->file_blk);
974
975 if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) {
976 return -1;
977 }
978 if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
979 &blkdev->xendev.remote_port) == -1) {
980 return -1;
981 }
982 if (xenstore_read_fe_int(&blkdev->xendev, "feature-persistent", &pers)) {
983 blkdev->feature_persistent = FALSE;
984 } else {
985 blkdev->feature_persistent = !!pers;
986 }
987
988 blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
989 if (blkdev->xendev.protocol) {
990 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
991 blkdev->protocol = BLKIF_PROTOCOL_X86_32;
992 }
993 if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
994 blkdev->protocol = BLKIF_PROTOCOL_X86_64;
995 }
996 }
997
998 blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
999 blkdev->xendev.dom,
1000 blkdev->ring_ref,
1001 PROT_READ | PROT_WRITE);
1002 if (!blkdev->sring) {
1003 return -1;
1004 }
1005 blkdev->cnt_map++;
1006
1007 switch (blkdev->protocol) {
1008 case BLKIF_PROTOCOL_NATIVE:
1009 {
1010 blkif_sring_t *sring_native = blkdev->sring;
1011 BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
1012 break;
1013 }
1014 case BLKIF_PROTOCOL_X86_32:
1015 {
1016 blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
1017
1018 BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
1019 break;
1020 }
1021 case BLKIF_PROTOCOL_X86_64:
1022 {
1023 blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
1024
1025 BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
1026 break;
1027 }
1028 }
1029
1030 if (blkdev->feature_persistent) {
1031 /* Init persistent grants */
1032 blkdev->max_grants = max_requests * BLKIF_MAX_SEGMENTS_PER_REQUEST;
1033 blkdev->persistent_gnts = g_tree_new_full((GCompareDataFunc)int_cmp,
1034 NULL, NULL,
1035 batch_maps ?
1036 (GDestroyNotify)g_free :
1037 (GDestroyNotify)destroy_grant);
1038 blkdev->persistent_regions = NULL;
1039 blkdev->persistent_gnt_count = 0;
1040 }
1041
1042 xen_be_bind_evtchn(&blkdev->xendev);
1043
1044 xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
1045 "remote port %d, local port %d\n",
1046 blkdev->xendev.protocol, blkdev->ring_ref,
1047 blkdev->xendev.remote_port, blkdev->xendev.local_port);
1048 return 0;
1049 }
1050
1051 static void blk_disconnect(struct XenDevice *xendev)
1052 {
1053 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1054
1055 if (blkdev->blk) {
1056 blk_detach_dev(blkdev->blk, blkdev);
1057 blk_unref(blkdev->blk);
1058 blkdev->blk = NULL;
1059 }
1060 xen_be_unbind_evtchn(&blkdev->xendev);
1061
1062 if (blkdev->sring) {
1063 xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
1064 blkdev->cnt_map--;
1065 blkdev->sring = NULL;
1066 }
1067
1068 /*
1069 * Unmap persistent grants before switching to the closed state
1070 * so the frontend can free them.
1071 *
1072 * In the !batch_maps case g_tree_destroy will take care of unmapping
1073 * the grant, but in the batch_maps case we need to iterate over every
1074 * region in persistent_regions and unmap it.
1075 */
1076 if (blkdev->feature_persistent) {
1077 g_tree_destroy(blkdev->persistent_gnts);
1078 assert(batch_maps || blkdev->persistent_gnt_count == 0);
1079 if (batch_maps) {
1080 blkdev->persistent_gnt_count = 0;
1081 g_slist_foreach(blkdev->persistent_regions,
1082 (GFunc)remove_persistent_region, blkdev);
1083 g_slist_free(blkdev->persistent_regions);
1084 }
1085 blkdev->feature_persistent = false;
1086 }
1087 }
1088
1089 static int blk_free(struct XenDevice *xendev)
1090 {
1091 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1092 struct ioreq *ioreq;
1093
1094 if (blkdev->blk || blkdev->sring) {
1095 blk_disconnect(xendev);
1096 }
1097
1098 while (!QLIST_EMPTY(&blkdev->freelist)) {
1099 ioreq = QLIST_FIRST(&blkdev->freelist);
1100 QLIST_REMOVE(ioreq, list);
1101 qemu_iovec_destroy(&ioreq->v);
1102 g_free(ioreq);
1103 }
1104
1105 g_free(blkdev->params);
1106 g_free(blkdev->mode);
1107 g_free(blkdev->type);
1108 g_free(blkdev->dev);
1109 g_free(blkdev->devtype);
1110 qemu_bh_delete(blkdev->bh);
1111 return 0;
1112 }
1113
1114 static void blk_event(struct XenDevice *xendev)
1115 {
1116 struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
1117
1118 qemu_bh_schedule(blkdev->bh);
1119 }
1120
1121 struct XenDevOps xen_blkdev_ops = {
1122 .size = sizeof(struct XenBlkDev),
1123 .flags = DEVOPS_FLAG_NEED_GNTDEV,
1124 .alloc = blk_alloc,
1125 .init = blk_init,
1126 .initialise = blk_connect,
1127 .disconnect = blk_disconnect,
1128 .event = blk_event,
1129 .free = blk_free,
1130 };