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