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