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