1 | /****************************************************************************** |
---|
2 | * arch/xen/drivers/blkif/backend/main.c |
---|
3 | * |
---|
4 | * Back-end of the driver for virtual block devices. This portion of the |
---|
5 | * driver exports a 'unified' block-device interface that can be accessed |
---|
6 | * by any operating system that implements a compatible front end. A |
---|
7 | * reference front-end implementation can be found in: |
---|
8 | * arch/xen/drivers/blkif/frontend |
---|
9 | * |
---|
10 | * Copyright (c) 2003-2004, Keir Fraser & Steve Hand |
---|
11 | * Copyright (c) 2005, Christopher Clark |
---|
12 | * |
---|
13 | * This program is free software; you can redistribute it and/or |
---|
14 | * modify it under the terms of the GNU General Public License version 2 |
---|
15 | * as published by the Free Software Foundation; or, when distributed |
---|
16 | * separately from the Linux kernel or incorporated into other |
---|
17 | * software packages, subject to the following license: |
---|
18 | * |
---|
19 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
20 | * of this source file (the "Software"), to deal in the Software without |
---|
21 | * restriction, including without limitation the rights to use, copy, modify, |
---|
22 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
---|
23 | * and to permit persons to whom the Software is furnished to do so, subject to |
---|
24 | * the following conditions: |
---|
25 | * |
---|
26 | * The above copyright notice and this permission notice shall be included in |
---|
27 | * all copies or substantial portions of the Software. |
---|
28 | * |
---|
29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
34 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
---|
35 | * IN THE SOFTWARE. |
---|
36 | */ |
---|
37 | |
---|
38 | #include <linux/spinlock.h> |
---|
39 | #include <linux/kthread.h> |
---|
40 | #include <linux/list.h> |
---|
41 | #include <xen/balloon.h> |
---|
42 | #include <asm/hypervisor.h> |
---|
43 | #include "common.h" |
---|
44 | |
---|
45 | /* |
---|
46 | * These are rather arbitrary. They are fairly large because adjacent requests |
---|
47 | * pulled from a communication ring are quite likely to end up being part of |
---|
48 | * the same scatter/gather request at the disc. |
---|
49 | * |
---|
50 | * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW ** |
---|
51 | * |
---|
52 | * This will increase the chances of being able to write whole tracks. |
---|
53 | * 64 should be enough to keep us competitive with Linux. |
---|
54 | */ |
---|
55 | static int blkif_reqs = 64; |
---|
56 | module_param_named(reqs, blkif_reqs, int, 0); |
---|
57 | MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate"); |
---|
58 | |
---|
59 | /* Run-time switchable: /sys/module/blkback/parameters/ */ |
---|
60 | static unsigned int log_stats = 0; |
---|
61 | static unsigned int debug_lvl = 0; |
---|
62 | module_param(log_stats, int, 0644); |
---|
63 | module_param(debug_lvl, int, 0644); |
---|
64 | |
---|
65 | /* |
---|
66 | * Each outstanding request that we've passed to the lower device layers has a |
---|
67 | * 'pending_req' allocated to it. Each buffer_head that completes decrements |
---|
68 | * the pendcnt towards zero. When it hits zero, the specified domain has a |
---|
69 | * response queued for it, with the saved 'id' passed back. |
---|
70 | */ |
---|
71 | typedef struct { |
---|
72 | blkif_t *blkif; |
---|
73 | u64 id; |
---|
74 | int nr_pages; |
---|
75 | atomic_t pendcnt; |
---|
76 | unsigned short operation; |
---|
77 | int status; |
---|
78 | struct list_head free_list; |
---|
79 | } pending_req_t; |
---|
80 | |
---|
81 | static pending_req_t *pending_reqs; |
---|
82 | static struct list_head pending_free; |
---|
83 | static DEFINE_SPINLOCK(pending_free_lock); |
---|
84 | static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq); |
---|
85 | |
---|
86 | #define BLKBACK_INVALID_HANDLE (~0) |
---|
87 | |
---|
88 | static struct page **pending_pages; |
---|
89 | static grant_handle_t *pending_grant_handles; |
---|
90 | |
---|
91 | static inline int vaddr_pagenr(pending_req_t *req, int seg) |
---|
92 | { |
---|
93 | return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg; |
---|
94 | } |
---|
95 | |
---|
96 | static inline unsigned long vaddr(pending_req_t *req, int seg) |
---|
97 | { |
---|
98 | unsigned long pfn = page_to_pfn(pending_pages[vaddr_pagenr(req, seg)]); |
---|
99 | return (unsigned long)pfn_to_kaddr(pfn); |
---|
100 | } |
---|
101 | |
---|
102 | #define pending_handle(_req, _seg) \ |
---|
103 | (pending_grant_handles[vaddr_pagenr(_req, _seg)]) |
---|
104 | |
---|
105 | |
---|
106 | static int do_block_io_op(blkif_t *blkif); |
---|
107 | static void dispatch_rw_block_io(blkif_t *blkif, |
---|
108 | blkif_request_t *req, |
---|
109 | pending_req_t *pending_req); |
---|
110 | static void make_response(blkif_t *blkif, u64 id, |
---|
111 | unsigned short op, int st); |
---|
112 | |
---|
113 | /****************************************************************** |
---|
114 | * misc small helpers |
---|
115 | */ |
---|
116 | static pending_req_t* alloc_req(void) |
---|
117 | { |
---|
118 | pending_req_t *req = NULL; |
---|
119 | unsigned long flags; |
---|
120 | |
---|
121 | spin_lock_irqsave(&pending_free_lock, flags); |
---|
122 | if (!list_empty(&pending_free)) { |
---|
123 | req = list_entry(pending_free.next, pending_req_t, free_list); |
---|
124 | list_del(&req->free_list); |
---|
125 | } |
---|
126 | spin_unlock_irqrestore(&pending_free_lock, flags); |
---|
127 | return req; |
---|
128 | } |
---|
129 | |
---|
130 | static void free_req(pending_req_t *req) |
---|
131 | { |
---|
132 | unsigned long flags; |
---|
133 | int was_empty; |
---|
134 | |
---|
135 | spin_lock_irqsave(&pending_free_lock, flags); |
---|
136 | was_empty = list_empty(&pending_free); |
---|
137 | list_add(&req->free_list, &pending_free); |
---|
138 | spin_unlock_irqrestore(&pending_free_lock, flags); |
---|
139 | if (was_empty) |
---|
140 | wake_up(&pending_free_wq); |
---|
141 | } |
---|
142 | |
---|
143 | static void unplug_queue(blkif_t *blkif) |
---|
144 | { |
---|
145 | if (blkif->plug == NULL) |
---|
146 | return; |
---|
147 | if (blkif->plug->unplug_fn) |
---|
148 | blkif->plug->unplug_fn(blkif->plug); |
---|
149 | blk_put_queue(blkif->plug); |
---|
150 | blkif->plug = NULL; |
---|
151 | } |
---|
152 | |
---|
153 | static void plug_queue(blkif_t *blkif, struct bio *bio) |
---|
154 | { |
---|
155 | request_queue_t *q = bdev_get_queue(bio->bi_bdev); |
---|
156 | |
---|
157 | if (q == blkif->plug) |
---|
158 | return; |
---|
159 | unplug_queue(blkif); |
---|
160 | blk_get_queue(q); |
---|
161 | blkif->plug = q; |
---|
162 | } |
---|
163 | |
---|
164 | static void fast_flush_area(pending_req_t *req) |
---|
165 | { |
---|
166 | struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
---|
167 | unsigned int i, invcount = 0; |
---|
168 | grant_handle_t handle; |
---|
169 | int ret; |
---|
170 | |
---|
171 | for (i = 0; i < req->nr_pages; i++) { |
---|
172 | handle = pending_handle(req, i); |
---|
173 | if (handle == BLKBACK_INVALID_HANDLE) |
---|
174 | continue; |
---|
175 | gnttab_set_unmap_op(&unmap[i], vaddr(req, i), GNTMAP_host_map, |
---|
176 | handle); |
---|
177 | pending_handle(req, i) = BLKBACK_INVALID_HANDLE; |
---|
178 | invcount++; |
---|
179 | } |
---|
180 | |
---|
181 | ret = HYPERVISOR_grant_table_op( |
---|
182 | GNTTABOP_unmap_grant_ref, unmap, invcount); |
---|
183 | BUG_ON(ret); |
---|
184 | } |
---|
185 | |
---|
186 | /****************************************************************** |
---|
187 | * SCHEDULER FUNCTIONS |
---|
188 | */ |
---|
189 | |
---|
190 | static void print_stats(blkif_t *blkif) |
---|
191 | { |
---|
192 | printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n", |
---|
193 | current->comm, blkif->st_oo_req, |
---|
194 | blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req); |
---|
195 | blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000); |
---|
196 | blkif->st_rd_req = 0; |
---|
197 | blkif->st_wr_req = 0; |
---|
198 | blkif->st_oo_req = 0; |
---|
199 | } |
---|
200 | |
---|
201 | int blkif_schedule(void *arg) |
---|
202 | { |
---|
203 | blkif_t *blkif = arg; |
---|
204 | |
---|
205 | blkif_get(blkif); |
---|
206 | |
---|
207 | if (debug_lvl) |
---|
208 | printk(KERN_DEBUG "%s: started\n", current->comm); |
---|
209 | |
---|
210 | while (!kthread_should_stop()) { |
---|
211 | wait_event_interruptible( |
---|
212 | blkif->wq, |
---|
213 | blkif->waiting_reqs || kthread_should_stop()); |
---|
214 | wait_event_interruptible( |
---|
215 | pending_free_wq, |
---|
216 | !list_empty(&pending_free) || kthread_should_stop()); |
---|
217 | |
---|
218 | blkif->waiting_reqs = 0; |
---|
219 | smp_mb(); /* clear flag *before* checking for work */ |
---|
220 | |
---|
221 | if (do_block_io_op(blkif)) |
---|
222 | blkif->waiting_reqs = 1; |
---|
223 | unplug_queue(blkif); |
---|
224 | |
---|
225 | if (log_stats && time_after(jiffies, blkif->st_print)) |
---|
226 | print_stats(blkif); |
---|
227 | } |
---|
228 | |
---|
229 | if (log_stats) |
---|
230 | print_stats(blkif); |
---|
231 | if (debug_lvl) |
---|
232 | printk(KERN_DEBUG "%s: exiting\n", current->comm); |
---|
233 | |
---|
234 | blkif->xenblkd = NULL; |
---|
235 | blkif_put(blkif); |
---|
236 | |
---|
237 | return 0; |
---|
238 | } |
---|
239 | |
---|
240 | /****************************************************************** |
---|
241 | * COMPLETION CALLBACK -- Called as bh->b_end_io() |
---|
242 | */ |
---|
243 | |
---|
244 | static void __end_block_io_op(pending_req_t *pending_req, int error) |
---|
245 | { |
---|
246 | /* An error fails the entire request. */ |
---|
247 | if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) && |
---|
248 | (error == -EOPNOTSUPP)) { |
---|
249 | DPRINTK("blkback: write barrier op failed, not supported\n"); |
---|
250 | blkback_barrier(XBT_NIL, pending_req->blkif->be, 0); |
---|
251 | pending_req->status = BLKIF_RSP_EOPNOTSUPP; |
---|
252 | } else if (error) { |
---|
253 | DPRINTK("Buffer not up-to-date at end of operation, " |
---|
254 | "error=%d\n", error); |
---|
255 | pending_req->status = BLKIF_RSP_ERROR; |
---|
256 | } |
---|
257 | |
---|
258 | if (atomic_dec_and_test(&pending_req->pendcnt)) { |
---|
259 | fast_flush_area(pending_req); |
---|
260 | make_response(pending_req->blkif, pending_req->id, |
---|
261 | pending_req->operation, pending_req->status); |
---|
262 | blkif_put(pending_req->blkif); |
---|
263 | free_req(pending_req); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | static int end_block_io_op(struct bio *bio, unsigned int done, int error) |
---|
268 | { |
---|
269 | if (bio->bi_size != 0) |
---|
270 | return 1; |
---|
271 | __end_block_io_op(bio->bi_private, error); |
---|
272 | bio_put(bio); |
---|
273 | return error; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | /****************************************************************************** |
---|
278 | * NOTIFICATION FROM GUEST OS. |
---|
279 | */ |
---|
280 | |
---|
281 | static void blkif_notify_work(blkif_t *blkif) |
---|
282 | { |
---|
283 | blkif->waiting_reqs = 1; |
---|
284 | wake_up(&blkif->wq); |
---|
285 | } |
---|
286 | |
---|
287 | irqreturn_t blkif_be_int(int irq, void *dev_id, struct pt_regs *regs) |
---|
288 | { |
---|
289 | blkif_notify_work(dev_id); |
---|
290 | return IRQ_HANDLED; |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | /****************************************************************** |
---|
296 | * DOWNWARD CALLS -- These interface with the block-device layer proper. |
---|
297 | */ |
---|
298 | |
---|
299 | static int do_block_io_op(blkif_t *blkif) |
---|
300 | { |
---|
301 | blkif_back_rings_t *blk_rings = &blkif->blk_rings; |
---|
302 | blkif_request_t req; |
---|
303 | pending_req_t *pending_req; |
---|
304 | RING_IDX rc, rp; |
---|
305 | int more_to_do = 0; |
---|
306 | |
---|
307 | rc = blk_rings->common.req_cons; |
---|
308 | rp = blk_rings->common.sring->req_prod; |
---|
309 | rmb(); /* Ensure we see queued requests up to 'rp'. */ |
---|
310 | |
---|
311 | while ((rc != rp)) { |
---|
312 | |
---|
313 | if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc)) |
---|
314 | break; |
---|
315 | |
---|
316 | pending_req = alloc_req(); |
---|
317 | if (NULL == pending_req) { |
---|
318 | blkif->st_oo_req++; |
---|
319 | more_to_do = 1; |
---|
320 | break; |
---|
321 | } |
---|
322 | |
---|
323 | switch (blkif->blk_protocol) { |
---|
324 | case BLKIF_PROTOCOL_NATIVE: |
---|
325 | memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req)); |
---|
326 | break; |
---|
327 | case BLKIF_PROTOCOL_X86_32: |
---|
328 | blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc)); |
---|
329 | break; |
---|
330 | case BLKIF_PROTOCOL_X86_64: |
---|
331 | blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc)); |
---|
332 | break; |
---|
333 | default: |
---|
334 | BUG(); |
---|
335 | } |
---|
336 | blk_rings->common.req_cons = ++rc; /* before make_response() */ |
---|
337 | |
---|
338 | switch (req.operation) { |
---|
339 | case BLKIF_OP_READ: |
---|
340 | blkif->st_rd_req++; |
---|
341 | dispatch_rw_block_io(blkif, &req, pending_req); |
---|
342 | break; |
---|
343 | case BLKIF_OP_WRITE_BARRIER: |
---|
344 | blkif->st_br_req++; |
---|
345 | /* fall through */ |
---|
346 | case BLKIF_OP_WRITE: |
---|
347 | blkif->st_wr_req++; |
---|
348 | dispatch_rw_block_io(blkif, &req, pending_req); |
---|
349 | break; |
---|
350 | default: |
---|
351 | DPRINTK("error: unknown block io operation [%d]\n", |
---|
352 | req.operation); |
---|
353 | make_response(blkif, req.id, req.operation, |
---|
354 | BLKIF_RSP_ERROR); |
---|
355 | free_req(pending_req); |
---|
356 | break; |
---|
357 | } |
---|
358 | } |
---|
359 | return more_to_do; |
---|
360 | } |
---|
361 | |
---|
362 | static void dispatch_rw_block_io(blkif_t *blkif, |
---|
363 | blkif_request_t *req, |
---|
364 | pending_req_t *pending_req) |
---|
365 | { |
---|
366 | extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]); |
---|
367 | struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
---|
368 | struct phys_req preq; |
---|
369 | struct { |
---|
370 | unsigned long buf; unsigned int nsec; |
---|
371 | } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
---|
372 | unsigned int nseg; |
---|
373 | struct bio *bio = NULL, *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
---|
374 | int ret, i, nbio = 0; |
---|
375 | int operation; |
---|
376 | |
---|
377 | switch (req->operation) { |
---|
378 | case BLKIF_OP_READ: |
---|
379 | operation = READ; |
---|
380 | break; |
---|
381 | case BLKIF_OP_WRITE: |
---|
382 | operation = WRITE; |
---|
383 | break; |
---|
384 | case BLKIF_OP_WRITE_BARRIER: |
---|
385 | operation = WRITE_BARRIER; |
---|
386 | break; |
---|
387 | default: |
---|
388 | operation = 0; /* make gcc happy */ |
---|
389 | BUG(); |
---|
390 | } |
---|
391 | |
---|
392 | /* Check that number of segments is sane. */ |
---|
393 | nseg = req->nr_segments; |
---|
394 | if (unlikely(nseg == 0) || |
---|
395 | unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) { |
---|
396 | DPRINTK("Bad number of segments in request (%d)\n", nseg); |
---|
397 | goto fail_response; |
---|
398 | } |
---|
399 | |
---|
400 | preq.dev = req->handle; |
---|
401 | preq.sector_number = req->sector_number; |
---|
402 | preq.nr_sects = 0; |
---|
403 | |
---|
404 | pending_req->blkif = blkif; |
---|
405 | pending_req->id = req->id; |
---|
406 | pending_req->operation = req->operation; |
---|
407 | pending_req->status = BLKIF_RSP_OKAY; |
---|
408 | pending_req->nr_pages = nseg; |
---|
409 | |
---|
410 | for (i = 0; i < nseg; i++) { |
---|
411 | uint32_t flags; |
---|
412 | |
---|
413 | seg[i].nsec = req->seg[i].last_sect - |
---|
414 | req->seg[i].first_sect + 1; |
---|
415 | |
---|
416 | if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) || |
---|
417 | (req->seg[i].last_sect < req->seg[i].first_sect)) |
---|
418 | goto fail_response; |
---|
419 | preq.nr_sects += seg[i].nsec; |
---|
420 | |
---|
421 | flags = GNTMAP_host_map; |
---|
422 | if (operation != READ) |
---|
423 | flags |= GNTMAP_readonly; |
---|
424 | gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags, |
---|
425 | req->seg[i].gref, blkif->domid); |
---|
426 | } |
---|
427 | |
---|
428 | ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg); |
---|
429 | BUG_ON(ret); |
---|
430 | |
---|
431 | for (i = 0; i < nseg; i++) { |
---|
432 | if (unlikely(map[i].status != 0)) { |
---|
433 | DPRINTK("invalid buffer -- could not remap it\n"); |
---|
434 | map[i].handle = BLKBACK_INVALID_HANDLE; |
---|
435 | ret |= 1; |
---|
436 | } |
---|
437 | |
---|
438 | pending_handle(pending_req, i) = map[i].handle; |
---|
439 | |
---|
440 | if (ret) |
---|
441 | continue; |
---|
442 | |
---|
443 | set_phys_to_machine(__pa(vaddr( |
---|
444 | pending_req, i)) >> PAGE_SHIFT, |
---|
445 | FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT)); |
---|
446 | seg[i].buf = map[i].dev_bus_addr | |
---|
447 | (req->seg[i].first_sect << 9); |
---|
448 | } |
---|
449 | |
---|
450 | if (ret) |
---|
451 | goto fail_flush; |
---|
452 | |
---|
453 | if (vbd_translate(&preq, blkif, operation) != 0) { |
---|
454 | DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n", |
---|
455 | operation == READ ? "read" : "write", |
---|
456 | preq.sector_number, |
---|
457 | preq.sector_number + preq.nr_sects, preq.dev); |
---|
458 | goto fail_flush; |
---|
459 | } |
---|
460 | |
---|
461 | for (i = 0; i < nseg; i++) { |
---|
462 | if (((int)preq.sector_number|(int)seg[i].nsec) & |
---|
463 | ((bdev_hardsect_size(preq.bdev) >> 9) - 1)) { |
---|
464 | DPRINTK("Misaligned I/O request from domain %d", |
---|
465 | blkif->domid); |
---|
466 | goto fail_put_bio; |
---|
467 | } |
---|
468 | |
---|
469 | while ((bio == NULL) || |
---|
470 | (bio_add_page(bio, |
---|
471 | virt_to_page(vaddr(pending_req, i)), |
---|
472 | seg[i].nsec << 9, |
---|
473 | seg[i].buf & ~PAGE_MASK) == 0)) { |
---|
474 | bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, nseg-i); |
---|
475 | if (unlikely(bio == NULL)) |
---|
476 | goto fail_put_bio; |
---|
477 | |
---|
478 | bio->bi_bdev = preq.bdev; |
---|
479 | bio->bi_private = pending_req; |
---|
480 | bio->bi_end_io = end_block_io_op; |
---|
481 | bio->bi_sector = preq.sector_number; |
---|
482 | } |
---|
483 | |
---|
484 | preq.sector_number += seg[i].nsec; |
---|
485 | } |
---|
486 | |
---|
487 | plug_queue(blkif, bio); |
---|
488 | atomic_set(&pending_req->pendcnt, nbio); |
---|
489 | blkif_get(blkif); |
---|
490 | |
---|
491 | for (i = 0; i < nbio; i++) |
---|
492 | submit_bio(operation, biolist[i]); |
---|
493 | |
---|
494 | if (operation == READ) |
---|
495 | blkif->st_rd_sect += preq.nr_sects; |
---|
496 | else if (operation == WRITE) |
---|
497 | blkif->st_wr_sect += preq.nr_sects; |
---|
498 | |
---|
499 | return; |
---|
500 | |
---|
501 | fail_put_bio: |
---|
502 | for (i = 0; i < (nbio-1); i++) |
---|
503 | bio_put(biolist[i]); |
---|
504 | fail_flush: |
---|
505 | fast_flush_area(pending_req); |
---|
506 | fail_response: |
---|
507 | make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR); |
---|
508 | free_req(pending_req); |
---|
509 | } |
---|
510 | |
---|
511 | |
---|
512 | |
---|
513 | /****************************************************************** |
---|
514 | * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING |
---|
515 | */ |
---|
516 | |
---|
517 | |
---|
518 | static void make_response(blkif_t *blkif, u64 id, |
---|
519 | unsigned short op, int st) |
---|
520 | { |
---|
521 | blkif_response_t resp; |
---|
522 | unsigned long flags; |
---|
523 | blkif_back_rings_t *blk_rings = &blkif->blk_rings; |
---|
524 | int more_to_do = 0; |
---|
525 | int notify; |
---|
526 | |
---|
527 | resp.id = id; |
---|
528 | resp.operation = op; |
---|
529 | resp.status = st; |
---|
530 | |
---|
531 | spin_lock_irqsave(&blkif->blk_ring_lock, flags); |
---|
532 | /* Place on the response ring for the relevant domain. */ |
---|
533 | switch (blkif->blk_protocol) { |
---|
534 | case BLKIF_PROTOCOL_NATIVE: |
---|
535 | memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt), |
---|
536 | &resp, sizeof(resp)); |
---|
537 | break; |
---|
538 | case BLKIF_PROTOCOL_X86_32: |
---|
539 | memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt), |
---|
540 | &resp, sizeof(resp)); |
---|
541 | break; |
---|
542 | case BLKIF_PROTOCOL_X86_64: |
---|
543 | memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt), |
---|
544 | &resp, sizeof(resp)); |
---|
545 | break; |
---|
546 | default: |
---|
547 | BUG(); |
---|
548 | } |
---|
549 | blk_rings->common.rsp_prod_pvt++; |
---|
550 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify); |
---|
551 | if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) { |
---|
552 | /* |
---|
553 | * Tail check for pending requests. Allows frontend to avoid |
---|
554 | * notifications if requests are already in flight (lower |
---|
555 | * overheads and promotes batching). |
---|
556 | */ |
---|
557 | RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do); |
---|
558 | |
---|
559 | } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) { |
---|
560 | more_to_do = 1; |
---|
561 | } |
---|
562 | |
---|
563 | spin_unlock_irqrestore(&blkif->blk_ring_lock, flags); |
---|
564 | |
---|
565 | if (more_to_do) |
---|
566 | blkif_notify_work(blkif); |
---|
567 | if (notify) |
---|
568 | notify_remote_via_irq(blkif->irq); |
---|
569 | } |
---|
570 | |
---|
571 | static int __init blkif_init(void) |
---|
572 | { |
---|
573 | int i, mmap_pages; |
---|
574 | |
---|
575 | if (!is_running_on_xen()) |
---|
576 | return -ENODEV; |
---|
577 | |
---|
578 | mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST; |
---|
579 | |
---|
580 | pending_reqs = kmalloc(sizeof(pending_reqs[0]) * |
---|
581 | blkif_reqs, GFP_KERNEL); |
---|
582 | pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) * |
---|
583 | mmap_pages, GFP_KERNEL); |
---|
584 | pending_pages = alloc_empty_pages_and_pagevec(mmap_pages); |
---|
585 | |
---|
586 | if (!pending_reqs || !pending_grant_handles || !pending_pages) |
---|
587 | goto out_of_memory; |
---|
588 | |
---|
589 | for (i = 0; i < mmap_pages; i++) |
---|
590 | pending_grant_handles[i] = BLKBACK_INVALID_HANDLE; |
---|
591 | |
---|
592 | blkif_interface_init(); |
---|
593 | |
---|
594 | memset(pending_reqs, 0, sizeof(pending_reqs)); |
---|
595 | INIT_LIST_HEAD(&pending_free); |
---|
596 | |
---|
597 | for (i = 0; i < blkif_reqs; i++) |
---|
598 | list_add_tail(&pending_reqs[i].free_list, &pending_free); |
---|
599 | |
---|
600 | blkif_xenbus_init(); |
---|
601 | |
---|
602 | return 0; |
---|
603 | |
---|
604 | out_of_memory: |
---|
605 | kfree(pending_reqs); |
---|
606 | kfree(pending_grant_handles); |
---|
607 | free_empty_pages_and_pagevec(pending_pages, mmap_pages); |
---|
608 | printk("%s: out of memory\n", __FUNCTION__); |
---|
609 | return -ENOMEM; |
---|
610 | } |
---|
611 | |
---|
612 | module_init(blkif_init); |
---|
613 | |
---|
614 | MODULE_LICENSE("Dual BSD/GPL"); |
---|