Commit | Line | Data |
---|---|---|
4d05a28d | 1 | /****************************************************************************** |
4d05a28d KRW |
2 | * |
3 | * Back-end of the driver for virtual block devices. This portion of the | |
4 | * driver exports a 'unified' block-device interface that can be accessed | |
5 | * by any operating system that implements a compatible front end. A | |
6 | * reference front-end implementation can be found in: | |
a1397fa3 | 7 | * drivers/block/xen-blkfront.c |
4d05a28d KRW |
8 | * |
9 | * Copyright (c) 2003-2004, Keir Fraser & Steve Hand | |
10 | * Copyright (c) 2005, Christopher Clark | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License version 2 | |
14 | * as published by the Free Software Foundation; or, when distributed | |
15 | * separately from the Linux kernel or incorporated into other | |
16 | * software packages, subject to the following license: | |
17 | * | |
18 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
19 | * of this source file (the "Software"), to deal in the Software without | |
20 | * restriction, including without limitation the rights to use, copy, modify, | |
21 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
22 | * and to permit persons to whom the Software is furnished to do so, subject to | |
23 | * the following conditions: | |
24 | * | |
25 | * The above copyright notice and this permission notice shall be included in | |
26 | * all copies or substantial portions of the Software. | |
27 | * | |
28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
29 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
30 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
31 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
32 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
33 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
34 | * IN THE SOFTWARE. | |
35 | */ | |
36 | ||
37 | #include <linux/spinlock.h> | |
38 | #include <linux/kthread.h> | |
39 | #include <linux/list.h> | |
40 | #include <linux/delay.h> | |
88122933 | 41 | #include <linux/freezer.h> |
0a8704a5 | 42 | #include <linux/bitmap.h> |
afd91d07 | 43 | |
88122933 JF |
44 | #include <xen/events.h> |
45 | #include <xen/page.h> | |
e79affc3 | 46 | #include <xen/xen.h> |
88122933 JF |
47 | #include <asm/xen/hypervisor.h> |
48 | #include <asm/xen/hypercall.h> | |
4d05a28d KRW |
49 | #include "common.h" |
50 | ||
51 | /* | |
52 | * These are rather arbitrary. They are fairly large because adjacent requests | |
53 | * pulled from a communication ring are quite likely to end up being part of | |
54 | * the same scatter/gather request at the disc. | |
55 | * | |
8b6bf747 | 56 | * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW ** |
4d05a28d KRW |
57 | * |
58 | * This will increase the chances of being able to write whole tracks. | |
59 | * 64 should be enough to keep us competitive with Linux. | |
60 | */ | |
8b6bf747 KRW |
61 | static int xen_blkif_reqs = 64; |
62 | module_param_named(reqs, xen_blkif_reqs, int, 0); | |
4d05a28d KRW |
63 | MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate"); |
64 | ||
65 | /* Run-time switchable: /sys/module/blkback/parameters/ */ | |
2e9977c2 | 66 | static unsigned int log_stats; |
4d05a28d | 67 | module_param(log_stats, int, 0644); |
4d05a28d KRW |
68 | |
69 | /* | |
70 | * Each outstanding request that we've passed to the lower device layers has a | |
71 | * 'pending_req' allocated to it. Each buffer_head that completes decrements | |
72 | * the pendcnt towards zero. When it hits zero, the specified domain has a | |
73 | * response queued for it, with the saved 'id' passed back. | |
74 | */ | |
2e9977c2 | 75 | struct pending_req { |
30fd1502 | 76 | struct xen_blkif *blkif; |
01f37f2d KRW |
77 | u64 id; |
78 | int nr_pages; | |
79 | atomic_t pendcnt; | |
80 | unsigned short operation; | |
81 | int status; | |
82 | struct list_head free_list; | |
0a8704a5 | 83 | DECLARE_BITMAP(unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
2e9977c2 | 84 | }; |
4d05a28d | 85 | |
4d05a28d KRW |
86 | #define BLKBACK_INVALID_HANDLE (~0) |
87 | ||
e8e28871 | 88 | struct xen_blkbk { |
2e9977c2 | 89 | struct pending_req *pending_reqs; |
a1397fa3 | 90 | /* List of all 'pending_req' available */ |
e8e28871 | 91 | struct list_head pending_free; |
a1397fa3 | 92 | /* And its spinlock. */ |
e8e28871 KRW |
93 | spinlock_t pending_free_lock; |
94 | wait_queue_head_t pending_free_wq; | |
a1397fa3 | 95 | /* The list of all pages that are available. */ |
e8e28871 | 96 | struct page **pending_pages; |
a1397fa3 | 97 | /* And the grant handles that are available. */ |
e8e28871 KRW |
98 | grant_handle_t *pending_grant_handles; |
99 | }; | |
100 | ||
101 | static struct xen_blkbk *blkbk; | |
4d05a28d | 102 | |
0a8704a5 RPM |
103 | /* |
104 | * Maximum number of grant pages that can be mapped in blkback. | |
105 | * BLKIF_MAX_SEGMENTS_PER_REQUEST * RING_SIZE is the maximum number of | |
106 | * pages that blkback will persistently map. | |
107 | * Currently, this is: | |
108 | * RING_SIZE = 32 (for all known ring types) | |
109 | * BLKIF_MAX_SEGMENTS_PER_REQUEST = 11 | |
110 | * sizeof(struct persistent_gnt) = 48 | |
111 | * So the maximum memory used to store the grants is: | |
112 | * 32 * 11 * 48 = 16896 bytes | |
113 | */ | |
114 | static inline unsigned int max_mapped_grant_pages(enum blkif_protocol protocol) | |
115 | { | |
116 | switch (protocol) { | |
117 | case BLKIF_PROTOCOL_NATIVE: | |
118 | return __CONST_RING_SIZE(blkif, PAGE_SIZE) * | |
119 | BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
120 | case BLKIF_PROTOCOL_X86_32: | |
121 | return __CONST_RING_SIZE(blkif_x86_32, PAGE_SIZE) * | |
122 | BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
123 | case BLKIF_PROTOCOL_X86_64: | |
124 | return __CONST_RING_SIZE(blkif_x86_64, PAGE_SIZE) * | |
125 | BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
126 | default: | |
127 | BUG(); | |
128 | } | |
129 | return 0; | |
130 | } | |
131 | ||
132 | ||
a1397fa3 KRW |
133 | /* |
134 | * Little helpful macro to figure out the index and virtual address of the | |
135 | * pending_pages[..]. For each 'pending_req' we have have up to | |
136 | * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through | |
01f37f2d KRW |
137 | * 10 and would index in the pending_pages[..]. |
138 | */ | |
2e9977c2 | 139 | static inline int vaddr_pagenr(struct pending_req *req, int seg) |
4d05a28d | 140 | { |
2e9977c2 KRW |
141 | return (req - blkbk->pending_reqs) * |
142 | BLKIF_MAX_SEGMENTS_PER_REQUEST + seg; | |
4d05a28d KRW |
143 | } |
144 | ||
efe08a3e JB |
145 | #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)] |
146 | ||
2e9977c2 | 147 | static inline unsigned long vaddr(struct pending_req *req, int seg) |
4d05a28d | 148 | { |
e8e28871 | 149 | unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg)); |
4d05a28d KRW |
150 | return (unsigned long)pfn_to_kaddr(pfn); |
151 | } | |
152 | ||
153 | #define pending_handle(_req, _seg) \ | |
e8e28871 | 154 | (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)]) |
4d05a28d KRW |
155 | |
156 | ||
30fd1502 KRW |
157 | static int do_block_io_op(struct xen_blkif *blkif); |
158 | static int dispatch_rw_block_io(struct xen_blkif *blkif, | |
fc53bf75 KRW |
159 | struct blkif_request *req, |
160 | struct pending_req *pending_req); | |
30fd1502 | 161 | static void make_response(struct xen_blkif *blkif, u64 id, |
4d05a28d KRW |
162 | unsigned short op, int st); |
163 | ||
0a8704a5 RPM |
164 | #define foreach_grant(pos, rbtree, node) \ |
165 | for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node); \ | |
166 | &(pos)->node != NULL; \ | |
167 | (pos) = container_of(rb_next(&(pos)->node), typeof(*(pos)), node)) | |
168 | ||
169 | ||
170 | static void add_persistent_gnt(struct rb_root *root, | |
171 | struct persistent_gnt *persistent_gnt) | |
172 | { | |
173 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
174 | struct persistent_gnt *this; | |
175 | ||
176 | /* Figure out where to put new node */ | |
177 | while (*new) { | |
178 | this = container_of(*new, struct persistent_gnt, node); | |
179 | ||
180 | parent = *new; | |
181 | if (persistent_gnt->gnt < this->gnt) | |
182 | new = &((*new)->rb_left); | |
183 | else if (persistent_gnt->gnt > this->gnt) | |
184 | new = &((*new)->rb_right); | |
185 | else { | |
186 | pr_alert(DRV_PFX " trying to add a gref that's already in the tree\n"); | |
187 | BUG(); | |
188 | } | |
189 | } | |
190 | ||
191 | /* Add new node and rebalance tree. */ | |
192 | rb_link_node(&(persistent_gnt->node), parent, new); | |
193 | rb_insert_color(&(persistent_gnt->node), root); | |
194 | } | |
195 | ||
196 | static struct persistent_gnt *get_persistent_gnt(struct rb_root *root, | |
197 | grant_ref_t gref) | |
198 | { | |
199 | struct persistent_gnt *data; | |
200 | struct rb_node *node = root->rb_node; | |
201 | ||
202 | while (node) { | |
203 | data = container_of(node, struct persistent_gnt, node); | |
204 | ||
205 | if (gref < data->gnt) | |
206 | node = node->rb_left; | |
207 | else if (gref > data->gnt) | |
208 | node = node->rb_right; | |
209 | else | |
210 | return data; | |
211 | } | |
212 | return NULL; | |
213 | } | |
214 | ||
a1397fa3 KRW |
215 | /* |
216 | * Retrieve from the 'pending_reqs' a free pending_req structure to be used. | |
4d05a28d | 217 | */ |
2e9977c2 | 218 | static struct pending_req *alloc_req(void) |
4d05a28d | 219 | { |
2e9977c2 | 220 | struct pending_req *req = NULL; |
4d05a28d KRW |
221 | unsigned long flags; |
222 | ||
e8e28871 KRW |
223 | spin_lock_irqsave(&blkbk->pending_free_lock, flags); |
224 | if (!list_empty(&blkbk->pending_free)) { | |
2e9977c2 KRW |
225 | req = list_entry(blkbk->pending_free.next, struct pending_req, |
226 | free_list); | |
4d05a28d KRW |
227 | list_del(&req->free_list); |
228 | } | |
e8e28871 | 229 | spin_unlock_irqrestore(&blkbk->pending_free_lock, flags); |
4d05a28d KRW |
230 | return req; |
231 | } | |
232 | ||
a1397fa3 KRW |
233 | /* |
234 | * Return the 'pending_req' structure back to the freepool. We also | |
235 | * wake up the thread if it was waiting for a free page. | |
236 | */ | |
2e9977c2 | 237 | static void free_req(struct pending_req *req) |
4d05a28d KRW |
238 | { |
239 | unsigned long flags; | |
240 | int was_empty; | |
241 | ||
e8e28871 KRW |
242 | spin_lock_irqsave(&blkbk->pending_free_lock, flags); |
243 | was_empty = list_empty(&blkbk->pending_free); | |
244 | list_add(&req->free_list, &blkbk->pending_free); | |
245 | spin_unlock_irqrestore(&blkbk->pending_free_lock, flags); | |
4d05a28d | 246 | if (was_empty) |
e8e28871 | 247 | wake_up(&blkbk->pending_free_wq); |
4d05a28d KRW |
248 | } |
249 | ||
ee9ff853 KRW |
250 | /* |
251 | * Routines for managing virtual block devices (vbds). | |
252 | */ | |
3d814731 KRW |
253 | static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif, |
254 | int operation) | |
ee9ff853 | 255 | { |
3d814731 | 256 | struct xen_vbd *vbd = &blkif->vbd; |
ee9ff853 KRW |
257 | int rc = -EACCES; |
258 | ||
259 | if ((operation != READ) && vbd->readonly) | |
260 | goto out; | |
261 | ||
8ab52150 JB |
262 | if (likely(req->nr_sects)) { |
263 | blkif_sector_t end = req->sector_number + req->nr_sects; | |
264 | ||
265 | if (unlikely(end < req->sector_number)) | |
266 | goto out; | |
267 | if (unlikely(end > vbd_sz(vbd))) | |
268 | goto out; | |
269 | } | |
ee9ff853 KRW |
270 | |
271 | req->dev = vbd->pdevice; | |
272 | req->bdev = vbd->bdev; | |
273 | rc = 0; | |
274 | ||
275 | out: | |
276 | return rc; | |
277 | } | |
278 | ||
3d814731 | 279 | static void xen_vbd_resize(struct xen_blkif *blkif) |
ee9ff853 | 280 | { |
3d814731 | 281 | struct xen_vbd *vbd = &blkif->vbd; |
ee9ff853 KRW |
282 | struct xenbus_transaction xbt; |
283 | int err; | |
8b6bf747 | 284 | struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be); |
42c7841d | 285 | unsigned long long new_size = vbd_sz(vbd); |
ee9ff853 | 286 | |
22b20f2d | 287 | pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n", |
ee9ff853 | 288 | blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice)); |
22b20f2d | 289 | pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size); |
ee9ff853 KRW |
290 | vbd->size = new_size; |
291 | again: | |
292 | err = xenbus_transaction_start(&xbt); | |
293 | if (err) { | |
22b20f2d | 294 | pr_warn(DRV_PFX "Error starting transaction"); |
ee9ff853 KRW |
295 | return; |
296 | } | |
297 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", | |
42c7841d | 298 | (unsigned long long)vbd_sz(vbd)); |
ee9ff853 | 299 | if (err) { |
22b20f2d | 300 | pr_warn(DRV_PFX "Error writing new size"); |
ee9ff853 KRW |
301 | goto abort; |
302 | } | |
303 | /* | |
304 | * Write the current state; we will use this to synchronize | |
305 | * the front-end. If the current state is "connected" the | |
306 | * front-end will get the new size information online. | |
307 | */ | |
308 | err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state); | |
309 | if (err) { | |
22b20f2d | 310 | pr_warn(DRV_PFX "Error writing the state"); |
ee9ff853 KRW |
311 | goto abort; |
312 | } | |
313 | ||
314 | err = xenbus_transaction_end(xbt, 0); | |
315 | if (err == -EAGAIN) | |
316 | goto again; | |
317 | if (err) | |
22b20f2d | 318 | pr_warn(DRV_PFX "Error ending transaction"); |
496b318e | 319 | return; |
ee9ff853 KRW |
320 | abort: |
321 | xenbus_transaction_end(xbt, 1); | |
322 | } | |
323 | ||
a1397fa3 | 324 | /* |
b0aef179 KRW |
325 | * Notification from the guest OS. |
326 | */ | |
30fd1502 | 327 | static void blkif_notify_work(struct xen_blkif *blkif) |
4d05a28d | 328 | { |
b0aef179 KRW |
329 | blkif->waiting_reqs = 1; |
330 | wake_up(&blkif->wq); | |
331 | } | |
4d05a28d | 332 | |
8b6bf747 | 333 | irqreturn_t xen_blkif_be_int(int irq, void *dev_id) |
b0aef179 KRW |
334 | { |
335 | blkif_notify_work(dev_id); | |
336 | return IRQ_HANDLED; | |
4d05a28d KRW |
337 | } |
338 | ||
2e9977c2 | 339 | /* |
4d05a28d KRW |
340 | * SCHEDULER FUNCTIONS |
341 | */ | |
342 | ||
30fd1502 | 343 | static void print_stats(struct xen_blkif *blkif) |
4d05a28d | 344 | { |
b3cb0d6a LD |
345 | pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d" |
346 | " | ds %4d\n", | |
ebe81906 | 347 | current->comm, blkif->st_oo_req, |
b3cb0d6a LD |
348 | blkif->st_rd_req, blkif->st_wr_req, |
349 | blkif->st_f_req, blkif->st_ds_req); | |
4d05a28d KRW |
350 | blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000); |
351 | blkif->st_rd_req = 0; | |
352 | blkif->st_wr_req = 0; | |
353 | blkif->st_oo_req = 0; | |
b3cb0d6a | 354 | blkif->st_ds_req = 0; |
4d05a28d KRW |
355 | } |
356 | ||
8b6bf747 | 357 | int xen_blkif_schedule(void *arg) |
4d05a28d | 358 | { |
30fd1502 | 359 | struct xen_blkif *blkif = arg; |
3d814731 | 360 | struct xen_vbd *vbd = &blkif->vbd; |
0a8704a5 RPM |
361 | struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
362 | struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
363 | struct persistent_gnt *persistent_gnt; | |
364 | int ret = 0; | |
365 | int segs_to_unmap = 0; | |
4d05a28d | 366 | |
8b6bf747 | 367 | xen_blkif_get(blkif); |
4d05a28d | 368 | |
4d05a28d KRW |
369 | while (!kthread_should_stop()) { |
370 | if (try_to_freeze()) | |
371 | continue; | |
42c7841d | 372 | if (unlikely(vbd->size != vbd_sz(vbd))) |
3d814731 | 373 | xen_vbd_resize(blkif); |
4d05a28d KRW |
374 | |
375 | wait_event_interruptible( | |
376 | blkif->wq, | |
377 | blkif->waiting_reqs || kthread_should_stop()); | |
378 | wait_event_interruptible( | |
e8e28871 | 379 | blkbk->pending_free_wq, |
2e9977c2 KRW |
380 | !list_empty(&blkbk->pending_free) || |
381 | kthread_should_stop()); | |
4d05a28d KRW |
382 | |
383 | blkif->waiting_reqs = 0; | |
384 | smp_mb(); /* clear flag *before* checking for work */ | |
385 | ||
386 | if (do_block_io_op(blkif)) | |
387 | blkif->waiting_reqs = 1; | |
4d05a28d KRW |
388 | |
389 | if (log_stats && time_after(jiffies, blkif->st_print)) | |
390 | print_stats(blkif); | |
391 | } | |
392 | ||
0a8704a5 RPM |
393 | /* Free all persistent grant pages */ |
394 | if (!RB_EMPTY_ROOT(&blkif->persistent_gnts)) { | |
395 | foreach_grant(persistent_gnt, &blkif->persistent_gnts, node) { | |
396 | BUG_ON(persistent_gnt->handle == | |
397 | BLKBACK_INVALID_HANDLE); | |
398 | gnttab_set_unmap_op(&unmap[segs_to_unmap], | |
399 | (unsigned long) pfn_to_kaddr(page_to_pfn( | |
400 | persistent_gnt->page)), | |
401 | GNTMAP_host_map, | |
402 | persistent_gnt->handle); | |
403 | ||
404 | pages[segs_to_unmap] = persistent_gnt->page; | |
405 | rb_erase(&persistent_gnt->node, | |
406 | &blkif->persistent_gnts); | |
407 | kfree(persistent_gnt); | |
408 | blkif->persistent_gnt_c--; | |
409 | ||
410 | if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST || | |
411 | !rb_next(&persistent_gnt->node)) { | |
412 | ret = gnttab_unmap_refs(unmap, NULL, pages, | |
413 | segs_to_unmap); | |
414 | BUG_ON(ret); | |
415 | segs_to_unmap = 0; | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | BUG_ON(blkif->persistent_gnt_c != 0); | |
421 | BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts)); | |
422 | ||
4d05a28d KRW |
423 | if (log_stats) |
424 | print_stats(blkif); | |
4d05a28d KRW |
425 | |
426 | blkif->xenblkd = NULL; | |
8b6bf747 | 427 | xen_blkif_put(blkif); |
4d05a28d KRW |
428 | |
429 | return 0; | |
430 | } | |
431 | ||
1a95fe6e KRW |
432 | struct seg_buf { |
433 | unsigned long buf; | |
434 | unsigned int nsec; | |
435 | }; | |
b0aef179 KRW |
436 | /* |
437 | * Unmap the grant references, and also remove the M2P over-rides | |
438 | * used in the 'pending_req'. | |
01f37f2d | 439 | */ |
9f3aedf5 | 440 | static void xen_blkbk_unmap(struct pending_req *req) |
b0aef179 KRW |
441 | { |
442 | struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
4f14faaa | 443 | struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
b0aef179 KRW |
444 | unsigned int i, invcount = 0; |
445 | grant_handle_t handle; | |
446 | int ret; | |
447 | ||
448 | for (i = 0; i < req->nr_pages; i++) { | |
0a8704a5 RPM |
449 | if (!test_bit(i, req->unmap_seg)) |
450 | continue; | |
b0aef179 KRW |
451 | handle = pending_handle(req, i); |
452 | if (handle == BLKBACK_INVALID_HANDLE) | |
453 | continue; | |
454 | gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i), | |
455 | GNTMAP_host_map, handle); | |
456 | pending_handle(req, i) = BLKBACK_INVALID_HANDLE; | |
4f14faaa | 457 | pages[invcount] = virt_to_page(vaddr(req, i)); |
b0aef179 KRW |
458 | invcount++; |
459 | } | |
460 | ||
2fc136ee | 461 | ret = gnttab_unmap_refs(unmap, NULL, pages, invcount); |
b0aef179 | 462 | BUG_ON(ret); |
b0aef179 | 463 | } |
01f37f2d KRW |
464 | |
465 | static int xen_blkbk_map(struct blkif_request *req, | |
466 | struct pending_req *pending_req, | |
0a8704a5 RPM |
467 | struct seg_buf seg[], |
468 | struct page *pages[]) | |
1a95fe6e KRW |
469 | { |
470 | struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
0a8704a5 RPM |
471 | struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
472 | struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
473 | struct persistent_gnt *persistent_gnt = NULL; | |
474 | struct xen_blkif *blkif = pending_req->blkif; | |
475 | phys_addr_t addr = 0; | |
476 | int i, j; | |
477 | bool new_map; | |
97e36834 | 478 | int nseg = req->u.rw.nr_segments; |
0a8704a5 | 479 | int segs_to_map = 0; |
1a95fe6e | 480 | int ret = 0; |
0a8704a5 RPM |
481 | int use_persistent_gnts; |
482 | ||
483 | use_persistent_gnts = (blkif->vbd.feature_gnt_persistent); | |
484 | ||
485 | BUG_ON(blkif->persistent_gnt_c > | |
486 | max_mapped_grant_pages(pending_req->blkif->blk_protocol)); | |
01f37f2d KRW |
487 | |
488 | /* | |
489 | * Fill out preq.nr_sects with proper amount of sectors, and setup | |
1a95fe6e KRW |
490 | * assign map[..] with the PFN of the page in our domain with the |
491 | * corresponding grant reference for each page. | |
492 | */ | |
493 | for (i = 0; i < nseg; i++) { | |
494 | uint32_t flags; | |
495 | ||
0a8704a5 RPM |
496 | if (use_persistent_gnts) |
497 | persistent_gnt = get_persistent_gnt( | |
498 | &blkif->persistent_gnts, | |
499 | req->u.rw.seg[i].gref); | |
500 | ||
501 | if (persistent_gnt) { | |
502 | /* | |
503 | * We are using persistent grants and | |
504 | * the grant is already mapped | |
505 | */ | |
506 | new_map = false; | |
507 | } else if (use_persistent_gnts && | |
508 | blkif->persistent_gnt_c < | |
509 | max_mapped_grant_pages(blkif->blk_protocol)) { | |
510 | /* | |
511 | * We are using persistent grants, the grant is | |
512 | * not mapped but we have room for it | |
513 | */ | |
514 | new_map = true; | |
cb5bd4d1 | 515 | persistent_gnt = kmalloc( |
0a8704a5 RPM |
516 | sizeof(struct persistent_gnt), |
517 | GFP_KERNEL); | |
518 | if (!persistent_gnt) | |
519 | return -ENOMEM; | |
520 | persistent_gnt->page = alloc_page(GFP_KERNEL); | |
521 | if (!persistent_gnt->page) { | |
522 | kfree(persistent_gnt); | |
523 | return -ENOMEM; | |
524 | } | |
525 | persistent_gnt->gnt = req->u.rw.seg[i].gref; | |
cb5bd4d1 | 526 | persistent_gnt->handle = BLKBACK_INVALID_HANDLE; |
0a8704a5 RPM |
527 | |
528 | pages_to_gnt[segs_to_map] = | |
529 | persistent_gnt->page; | |
530 | addr = (unsigned long) pfn_to_kaddr( | |
531 | page_to_pfn(persistent_gnt->page)); | |
532 | ||
533 | add_persistent_gnt(&blkif->persistent_gnts, | |
534 | persistent_gnt); | |
535 | blkif->persistent_gnt_c++; | |
536 | pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n", | |
537 | persistent_gnt->gnt, blkif->persistent_gnt_c, | |
538 | max_mapped_grant_pages(blkif->blk_protocol)); | |
539 | } else { | |
540 | /* | |
541 | * We are either using persistent grants and | |
542 | * hit the maximum limit of grants mapped, | |
543 | * or we are not using persistent grants. | |
544 | */ | |
545 | if (use_persistent_gnts && | |
546 | !blkif->vbd.overflow_max_grants) { | |
547 | blkif->vbd.overflow_max_grants = 1; | |
548 | pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n", | |
549 | blkif->domid, blkif->vbd.handle); | |
550 | } | |
551 | new_map = true; | |
552 | pages[i] = blkbk->pending_page(pending_req, i); | |
553 | addr = vaddr(pending_req, i); | |
554 | pages_to_gnt[segs_to_map] = | |
555 | blkbk->pending_page(pending_req, i); | |
556 | } | |
557 | ||
558 | if (persistent_gnt) { | |
559 | pages[i] = persistent_gnt->page; | |
560 | persistent_gnts[i] = persistent_gnt; | |
561 | } else { | |
562 | persistent_gnts[i] = NULL; | |
563 | } | |
564 | ||
565 | if (new_map) { | |
566 | flags = GNTMAP_host_map; | |
567 | if (!persistent_gnt && | |
568 | (pending_req->operation != BLKIF_OP_READ)) | |
569 | flags |= GNTMAP_readonly; | |
570 | gnttab_set_map_op(&map[segs_to_map++], addr, | |
571 | flags, req->u.rw.seg[i].gref, | |
572 | blkif->domid); | |
573 | } | |
1a95fe6e KRW |
574 | } |
575 | ||
0a8704a5 RPM |
576 | if (segs_to_map) { |
577 | ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map); | |
578 | BUG_ON(ret); | |
579 | } | |
1a95fe6e | 580 | |
01f37f2d KRW |
581 | /* |
582 | * Now swizzle the MFN in our domain with the MFN from the other domain | |
1a95fe6e KRW |
583 | * so that when we access vaddr(pending_req,i) it has the contents of |
584 | * the page from the other domain. | |
585 | */ | |
0a8704a5 RPM |
586 | bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
587 | for (i = 0, j = 0; i < nseg; i++) { | |
cb5bd4d1 RPM |
588 | if (!persistent_gnts[i] || |
589 | persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) { | |
0a8704a5 RPM |
590 | /* This is a newly mapped grant */ |
591 | BUG_ON(j >= segs_to_map); | |
592 | if (unlikely(map[j].status != 0)) { | |
593 | pr_debug(DRV_PFX "invalid buffer -- could not remap it\n"); | |
594 | map[j].handle = BLKBACK_INVALID_HANDLE; | |
595 | ret |= 1; | |
596 | if (persistent_gnts[i]) { | |
597 | rb_erase(&persistent_gnts[i]->node, | |
598 | &blkif->persistent_gnts); | |
599 | blkif->persistent_gnt_c--; | |
600 | kfree(persistent_gnts[i]); | |
601 | persistent_gnts[i] = NULL; | |
602 | } | |
603 | } | |
604 | } | |
605 | if (persistent_gnts[i]) { | |
cb5bd4d1 RPM |
606 | if (persistent_gnts[i]->handle == |
607 | BLKBACK_INVALID_HANDLE) { | |
0a8704a5 RPM |
608 | /* |
609 | * If this is a new persistent grant | |
610 | * save the handler | |
611 | */ | |
612 | persistent_gnts[i]->handle = map[j].handle; | |
613 | persistent_gnts[i]->dev_bus_addr = | |
614 | map[j++].dev_bus_addr; | |
615 | } | |
616 | pending_handle(pending_req, i) = | |
617 | persistent_gnts[i]->handle; | |
618 | ||
619 | if (ret) | |
620 | continue; | |
621 | ||
622 | seg[i].buf = persistent_gnts[i]->dev_bus_addr | | |
623 | (req->u.rw.seg[i].first_sect << 9); | |
624 | } else { | |
625 | pending_handle(pending_req, i) = map[j].handle; | |
626 | bitmap_set(pending_req->unmap_seg, i, 1); | |
627 | ||
628 | if (ret) { | |
629 | j++; | |
630 | continue; | |
631 | } | |
632 | ||
633 | seg[i].buf = map[j++].dev_bus_addr | | |
634 | (req->u.rw.seg[i].first_sect << 9); | |
1a95fe6e | 635 | } |
1a95fe6e KRW |
636 | } |
637 | return ret; | |
638 | } | |
639 | ||
42146352 KRW |
640 | static int dispatch_discard_io(struct xen_blkif *blkif, |
641 | struct blkif_request *req) | |
b3cb0d6a LD |
642 | { |
643 | int err = 0; | |
644 | int status = BLKIF_RSP_OKAY; | |
645 | struct block_device *bdev = blkif->vbd.bdev; | |
4dae7670 | 646 | unsigned long secure; |
b3cb0d6a | 647 | |
42146352 KRW |
648 | blkif->st_ds_req++; |
649 | ||
650 | xen_blkif_get(blkif); | |
4dae7670 KRW |
651 | secure = (blkif->vbd.discard_secure && |
652 | (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ? | |
653 | BLKDEV_DISCARD_SECURE : 0; | |
654 | ||
655 | err = blkdev_issue_discard(bdev, req->u.discard.sector_number, | |
656 | req->u.discard.nr_sectors, | |
657 | GFP_KERNEL, secure); | |
b3cb0d6a LD |
658 | |
659 | if (err == -EOPNOTSUPP) { | |
660 | pr_debug(DRV_PFX "discard op failed, not supported\n"); | |
661 | status = BLKIF_RSP_EOPNOTSUPP; | |
662 | } else if (err) | |
663 | status = BLKIF_RSP_ERROR; | |
664 | ||
97e36834 | 665 | make_response(blkif, req->u.discard.id, req->operation, status); |
42146352 KRW |
666 | xen_blkif_put(blkif); |
667 | return err; | |
b3cb0d6a LD |
668 | } |
669 | ||
29bde093 KRW |
670 | static void xen_blk_drain_io(struct xen_blkif *blkif) |
671 | { | |
672 | atomic_set(&blkif->drain, 1); | |
673 | do { | |
6927d920 KRW |
674 | /* The initial value is one, and one refcnt taken at the |
675 | * start of the xen_blkif_schedule thread. */ | |
676 | if (atomic_read(&blkif->refcnt) <= 2) | |
677 | break; | |
29bde093 KRW |
678 | wait_for_completion_interruptible_timeout( |
679 | &blkif->drain_complete, HZ); | |
680 | ||
681 | if (!atomic_read(&blkif->drain)) | |
682 | break; | |
29bde093 KRW |
683 | } while (!kthread_should_stop()); |
684 | atomic_set(&blkif->drain, 0); | |
685 | } | |
686 | ||
a1397fa3 KRW |
687 | /* |
688 | * Completion callback on the bio's. Called as bh->b_end_io() | |
4d05a28d KRW |
689 | */ |
690 | ||
2e9977c2 | 691 | static void __end_block_io_op(struct pending_req *pending_req, int error) |
4d05a28d KRW |
692 | { |
693 | /* An error fails the entire request. */ | |
24f567f9 | 694 | if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) && |
4d05a28d | 695 | (error == -EOPNOTSUPP)) { |
22b20f2d | 696 | pr_debug(DRV_PFX "flush diskcache op failed, not supported\n"); |
24f567f9 | 697 | xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0); |
4d05a28d | 698 | pending_req->status = BLKIF_RSP_EOPNOTSUPP; |
29bde093 KRW |
699 | } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) && |
700 | (error == -EOPNOTSUPP)) { | |
701 | pr_debug(DRV_PFX "write barrier op failed, not supported\n"); | |
702 | xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0); | |
703 | pending_req->status = BLKIF_RSP_EOPNOTSUPP; | |
4d05a28d | 704 | } else if (error) { |
22b20f2d | 705 | pr_debug(DRV_PFX "Buffer not up-to-date at end of operation," |
ebe81906 | 706 | " error=%d\n", error); |
4d05a28d KRW |
707 | pending_req->status = BLKIF_RSP_ERROR; |
708 | } | |
709 | ||
01f37f2d KRW |
710 | /* |
711 | * If all of the bio's have completed it is time to unmap | |
a1397fa3 | 712 | * the grant references associated with 'request' and provide |
2e9977c2 KRW |
713 | * the proper response on the ring. |
714 | */ | |
4d05a28d | 715 | if (atomic_dec_and_test(&pending_req->pendcnt)) { |
9f3aedf5 | 716 | xen_blkbk_unmap(pending_req); |
4d05a28d KRW |
717 | make_response(pending_req->blkif, pending_req->id, |
718 | pending_req->operation, pending_req->status); | |
8b6bf747 | 719 | xen_blkif_put(pending_req->blkif); |
29bde093 KRW |
720 | if (atomic_read(&pending_req->blkif->refcnt) <= 2) { |
721 | if (atomic_read(&pending_req->blkif->drain)) | |
722 | complete(&pending_req->blkif->drain_complete); | |
723 | } | |
4d05a28d KRW |
724 | free_req(pending_req); |
725 | } | |
726 | } | |
727 | ||
a1397fa3 KRW |
728 | /* |
729 | * bio callback. | |
730 | */ | |
88122933 | 731 | static void end_block_io_op(struct bio *bio, int error) |
4d05a28d | 732 | { |
4d05a28d KRW |
733 | __end_block_io_op(bio->bi_private, error); |
734 | bio_put(bio); | |
4d05a28d KRW |
735 | } |
736 | ||
737 | ||
4d05a28d | 738 | |
a1397fa3 KRW |
739 | /* |
740 | * Function to copy the from the ring buffer the 'struct blkif_request' | |
741 | * (which has the sectors we want, number of them, grant references, etc), | |
742 | * and transmute it to the block API to hand it over to the proper block disk. | |
4d05a28d | 743 | */ |
b4726a9d DS |
744 | static int |
745 | __do_block_io_op(struct xen_blkif *blkif) | |
4d05a28d | 746 | { |
88122933 JF |
747 | union blkif_back_rings *blk_rings = &blkif->blk_rings; |
748 | struct blkif_request req; | |
2e9977c2 | 749 | struct pending_req *pending_req; |
4d05a28d KRW |
750 | RING_IDX rc, rp; |
751 | int more_to_do = 0; | |
752 | ||
753 | rc = blk_rings->common.req_cons; | |
754 | rp = blk_rings->common.sring->req_prod; | |
755 | rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
756 | ||
757 | while (rc != rp) { | |
758 | ||
759 | if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc)) | |
760 | break; | |
761 | ||
8270b45b | 762 | if (kthread_should_stop()) { |
4d05a28d KRW |
763 | more_to_do = 1; |
764 | break; | |
765 | } | |
766 | ||
8270b45b KF |
767 | pending_req = alloc_req(); |
768 | if (NULL == pending_req) { | |
769 | blkif->st_oo_req++; | |
4d05a28d KRW |
770 | more_to_do = 1; |
771 | break; | |
772 | } | |
773 | ||
774 | switch (blkif->blk_protocol) { | |
775 | case BLKIF_PROTOCOL_NATIVE: | |
776 | memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req)); | |
777 | break; | |
778 | case BLKIF_PROTOCOL_X86_32: | |
779 | blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc)); | |
780 | break; | |
781 | case BLKIF_PROTOCOL_X86_64: | |
782 | blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc)); | |
783 | break; | |
784 | default: | |
785 | BUG(); | |
786 | } | |
787 | blk_rings->common.req_cons = ++rc; /* before make_response() */ | |
788 | ||
789 | /* Apply all sanity checks to /private copy/ of request. */ | |
790 | barrier(); | |
42146352 KRW |
791 | if (unlikely(req.operation == BLKIF_OP_DISCARD)) { |
792 | free_req(pending_req); | |
793 | if (dispatch_discard_io(blkif, &req)) | |
794 | break; | |
795 | } else if (dispatch_rw_block_io(blkif, &req, pending_req)) | |
4d05a28d | 796 | break; |
4d05a28d KRW |
797 | |
798 | /* Yield point for this unbounded loop. */ | |
799 | cond_resched(); | |
800 | } | |
801 | ||
802 | return more_to_do; | |
803 | } | |
804 | ||
b4726a9d DS |
805 | static int |
806 | do_block_io_op(struct xen_blkif *blkif) | |
807 | { | |
808 | union blkif_back_rings *blk_rings = &blkif->blk_rings; | |
809 | int more_to_do; | |
810 | ||
811 | do { | |
812 | more_to_do = __do_block_io_op(blkif); | |
813 | if (more_to_do) | |
814 | break; | |
815 | ||
816 | RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do); | |
817 | } while (more_to_do); | |
818 | ||
819 | return more_to_do; | |
820 | } | |
a1397fa3 | 821 | /* |
01f37f2d KRW |
822 | * Transmutation of the 'struct blkif_request' to a proper 'struct bio' |
823 | * and call the 'submit_bio' to pass it to the underlying storage. | |
a1397fa3 | 824 | */ |
30fd1502 KRW |
825 | static int dispatch_rw_block_io(struct xen_blkif *blkif, |
826 | struct blkif_request *req, | |
827 | struct pending_req *pending_req) | |
4d05a28d | 828 | { |
4d05a28d | 829 | struct phys_req preq; |
1a95fe6e | 830 | struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
4d05a28d KRW |
831 | unsigned int nseg; |
832 | struct bio *bio = NULL; | |
77089926 | 833 | struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
1a95fe6e | 834 | int i, nbio = 0; |
4d05a28d | 835 | int operation; |
a19be5f0 | 836 | struct blk_plug plug; |
29bde093 | 837 | bool drain = false; |
0a8704a5 | 838 | struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
4d05a28d KRW |
839 | |
840 | switch (req->operation) { | |
841 | case BLKIF_OP_READ: | |
fc53bf75 | 842 | blkif->st_rd_req++; |
4d05a28d KRW |
843 | operation = READ; |
844 | break; | |
845 | case BLKIF_OP_WRITE: | |
fc53bf75 | 846 | blkif->st_wr_req++; |
013c3ca1 | 847 | operation = WRITE_ODIRECT; |
4d05a28d | 848 | break; |
29bde093 KRW |
849 | case BLKIF_OP_WRITE_BARRIER: |
850 | drain = true; | |
24f567f9 | 851 | case BLKIF_OP_FLUSH_DISKCACHE: |
fc53bf75 | 852 | blkif->st_f_req++; |
24f567f9 | 853 | operation = WRITE_FLUSH; |
4d05a28d KRW |
854 | break; |
855 | default: | |
856 | operation = 0; /* make gcc happy */ | |
fc53bf75 KRW |
857 | goto fail_response; |
858 | break; | |
4d05a28d KRW |
859 | } |
860 | ||
42146352 KRW |
861 | /* Check that the number of segments is sane. */ |
862 | nseg = req->u.rw.nr_segments; | |
97e36834 | 863 | |
42146352 | 864 | if (unlikely(nseg == 0 && operation != WRITE_FLUSH) || |
4d05a28d | 865 | unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) { |
22b20f2d | 866 | pr_debug(DRV_PFX "Bad number of segments in request (%d)\n", |
ebe81906 | 867 | nseg); |
1a95fe6e | 868 | /* Haven't submitted any bio's yet. */ |
4d05a28d KRW |
869 | goto fail_response; |
870 | } | |
871 | ||
97e36834 | 872 | preq.dev = req->u.rw.handle; |
c35950bf | 873 | preq.sector_number = req->u.rw.sector_number; |
4d05a28d KRW |
874 | preq.nr_sects = 0; |
875 | ||
876 | pending_req->blkif = blkif; | |
97e36834 | 877 | pending_req->id = req->u.rw.id; |
4d05a28d KRW |
878 | pending_req->operation = req->operation; |
879 | pending_req->status = BLKIF_RSP_OKAY; | |
880 | pending_req->nr_pages = nseg; | |
e9350493 | 881 | |
4d05a28d | 882 | for (i = 0; i < nseg; i++) { |
c35950bf KRW |
883 | seg[i].nsec = req->u.rw.seg[i].last_sect - |
884 | req->u.rw.seg[i].first_sect + 1; | |
c35950bf KRW |
885 | if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) || |
886 | (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect)) | |
4d05a28d KRW |
887 | goto fail_response; |
888 | preq.nr_sects += seg[i].nsec; | |
976222e0 | 889 | |
4d05a28d KRW |
890 | } |
891 | ||
3d814731 | 892 | if (xen_vbd_translate(&preq, blkif, operation) != 0) { |
22b20f2d | 893 | pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n", |
ebe81906 KRW |
894 | operation == READ ? "read" : "write", |
895 | preq.sector_number, | |
896 | preq.sector_number + preq.nr_sects, preq.dev); | |
1a95fe6e | 897 | goto fail_response; |
4d05a28d | 898 | } |
01f37f2d KRW |
899 | |
900 | /* | |
3d814731 | 901 | * This check _MUST_ be done after xen_vbd_translate as the preq.bdev |
01f37f2d KRW |
902 | * is set there. |
903 | */ | |
e9350493 KRW |
904 | for (i = 0; i < nseg; i++) { |
905 | if (((int)preq.sector_number|(int)seg[i].nsec) & | |
906 | ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) { | |
22b20f2d | 907 | pr_debug(DRV_PFX "Misaligned I/O request from domain %d", |
ebe81906 | 908 | blkif->domid); |
e9350493 KRW |
909 | goto fail_response; |
910 | } | |
911 | } | |
01f37f2d | 912 | |
29bde093 KRW |
913 | /* Wait on all outstanding I/O's and once that has been completed |
914 | * issue the WRITE_FLUSH. | |
915 | */ | |
916 | if (drain) | |
917 | xen_blk_drain_io(pending_req->blkif); | |
918 | ||
01f37f2d KRW |
919 | /* |
920 | * If we have failed at this point, we need to undo the M2P override, | |
2e9977c2 KRW |
921 | * set gnttab_set_unmap_op on all of the grant references and perform |
922 | * the hypercall to unmap the grants - that is all done in | |
9f3aedf5 | 923 | * xen_blkbk_unmap. |
2e9977c2 | 924 | */ |
0a8704a5 | 925 | if (xen_blkbk_map(req, pending_req, seg, pages)) |
4d05a28d KRW |
926 | goto fail_flush; |
927 | ||
b3cb0d6a LD |
928 | /* |
929 | * This corresponding xen_blkif_put is done in __end_block_io_op, or | |
930 | * below (in "!bio") if we are handling a BLKIF_OP_DISCARD. | |
931 | */ | |
8b6bf747 | 932 | xen_blkif_get(blkif); |
4d05a28d KRW |
933 | |
934 | for (i = 0; i < nseg; i++) { | |
4d05a28d KRW |
935 | while ((bio == NULL) || |
936 | (bio_add_page(bio, | |
0a8704a5 | 937 | pages[i], |
4d05a28d KRW |
938 | seg[i].nsec << 9, |
939 | seg[i].buf & ~PAGE_MASK) == 0)) { | |
2e9977c2 | 940 | |
03e0edf9 | 941 | bio = bio_alloc(GFP_KERNEL, nseg-i); |
4d05a28d KRW |
942 | if (unlikely(bio == NULL)) |
943 | goto fail_put_bio; | |
944 | ||
03e0edf9 | 945 | biolist[nbio++] = bio; |
4d05a28d KRW |
946 | bio->bi_bdev = preq.bdev; |
947 | bio->bi_private = pending_req; | |
948 | bio->bi_end_io = end_block_io_op; | |
949 | bio->bi_sector = preq.sector_number; | |
950 | } | |
951 | ||
952 | preq.sector_number += seg[i].nsec; | |
953 | } | |
954 | ||
b3cb0d6a | 955 | /* This will be hit if the operation was a flush or discard. */ |
4d05a28d | 956 | if (!bio) { |
42146352 | 957 | BUG_ON(operation != WRITE_FLUSH); |
b0f80127 | 958 | |
42146352 KRW |
959 | bio = bio_alloc(GFP_KERNEL, 0); |
960 | if (unlikely(bio == NULL)) | |
961 | goto fail_put_bio; | |
4d05a28d | 962 | |
42146352 KRW |
963 | biolist[nbio++] = bio; |
964 | bio->bi_bdev = preq.bdev; | |
965 | bio->bi_private = pending_req; | |
966 | bio->bi_end_io = end_block_io_op; | |
4d05a28d KRW |
967 | } |
968 | ||
01f37f2d KRW |
969 | /* |
970 | * We set it one so that the last submit_bio does not have to call | |
77089926 KRW |
971 | * atomic_inc. |
972 | */ | |
973 | atomic_set(&pending_req->pendcnt, nbio); | |
974 | ||
a19be5f0 KRW |
975 | /* Get a reference count for the disk queue and start sending I/O */ |
976 | blk_start_plug(&plug); | |
977 | ||
77089926 KRW |
978 | for (i = 0; i < nbio; i++) |
979 | submit_bio(operation, biolist[i]); | |
980 | ||
a19be5f0 | 981 | /* Let the I/Os go.. */ |
3d68b399 | 982 | blk_finish_plug(&plug); |
a19be5f0 | 983 | |
4d05a28d KRW |
984 | if (operation == READ) |
985 | blkif->st_rd_sect += preq.nr_sects; | |
5c62cb48 | 986 | else if (operation & WRITE) |
4d05a28d KRW |
987 | blkif->st_wr_sect += preq.nr_sects; |
988 | ||
fc53bf75 | 989 | return 0; |
4d05a28d KRW |
990 | |
991 | fail_flush: | |
9f3aedf5 | 992 | xen_blkbk_unmap(pending_req); |
4d05a28d | 993 | fail_response: |
0faa8cca | 994 | /* Haven't submitted any bio's yet. */ |
97e36834 | 995 | make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR); |
4d05a28d KRW |
996 | free_req(pending_req); |
997 | msleep(1); /* back off a bit */ | |
fc53bf75 | 998 | return -EIO; |
4d05a28d KRW |
999 | |
1000 | fail_put_bio: | |
03e0edf9 | 1001 | for (i = 0; i < nbio; i++) |
77089926 | 1002 | bio_put(biolist[i]); |
4d05a28d | 1003 | __end_block_io_op(pending_req, -EINVAL); |
4d05a28d | 1004 | msleep(1); /* back off a bit */ |
fc53bf75 | 1005 | return -EIO; |
4d05a28d KRW |
1006 | } |
1007 | ||
1008 | ||
1009 | ||
a1397fa3 KRW |
1010 | /* |
1011 | * Put a response on the ring on how the operation fared. | |
4d05a28d | 1012 | */ |
30fd1502 | 1013 | static void make_response(struct xen_blkif *blkif, u64 id, |
4d05a28d KRW |
1014 | unsigned short op, int st) |
1015 | { | |
88122933 | 1016 | struct blkif_response resp; |
4d05a28d | 1017 | unsigned long flags; |
88122933 | 1018 | union blkif_back_rings *blk_rings = &blkif->blk_rings; |
4d05a28d KRW |
1019 | int notify; |
1020 | ||
1021 | resp.id = id; | |
1022 | resp.operation = op; | |
1023 | resp.status = st; | |
1024 | ||
1025 | spin_lock_irqsave(&blkif->blk_ring_lock, flags); | |
1026 | /* Place on the response ring for the relevant domain. */ | |
1027 | switch (blkif->blk_protocol) { | |
1028 | case BLKIF_PROTOCOL_NATIVE: | |
1029 | memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt), | |
1030 | &resp, sizeof(resp)); | |
1031 | break; | |
1032 | case BLKIF_PROTOCOL_X86_32: | |
1033 | memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt), | |
1034 | &resp, sizeof(resp)); | |
1035 | break; | |
1036 | case BLKIF_PROTOCOL_X86_64: | |
1037 | memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt), | |
1038 | &resp, sizeof(resp)); | |
1039 | break; | |
1040 | default: | |
1041 | BUG(); | |
1042 | } | |
1043 | blk_rings->common.rsp_prod_pvt++; | |
1044 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify); | |
4d05a28d | 1045 | spin_unlock_irqrestore(&blkif->blk_ring_lock, flags); |
4d05a28d KRW |
1046 | if (notify) |
1047 | notify_remote_via_irq(blkif->irq); | |
1048 | } | |
1049 | ||
8b6bf747 | 1050 | static int __init xen_blkif_init(void) |
4d05a28d KRW |
1051 | { |
1052 | int i, mmap_pages; | |
8770b268 | 1053 | int rc = 0; |
4d05a28d | 1054 | |
b2167ba6 | 1055 | if (!xen_domain()) |
4d05a28d KRW |
1056 | return -ENODEV; |
1057 | ||
2e9977c2 | 1058 | blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL); |
e8e28871 | 1059 | if (!blkbk) { |
22b20f2d | 1060 | pr_alert(DRV_PFX "%s: out of memory!\n", __func__); |
e8e28871 KRW |
1061 | return -ENOMEM; |
1062 | } | |
1063 | ||
8b6bf747 | 1064 | mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST; |
4d05a28d | 1065 | |
8e6dc6fe | 1066 | blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) * |
8b6bf747 | 1067 | xen_blkif_reqs, GFP_KERNEL); |
8e6dc6fe | 1068 | blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) * |
a742b02c KRW |
1069 | mmap_pages, GFP_KERNEL); |
1070 | blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) * | |
1071 | mmap_pages, GFP_KERNEL); | |
4d05a28d | 1072 | |
2e9977c2 KRW |
1073 | if (!blkbk->pending_reqs || !blkbk->pending_grant_handles || |
1074 | !blkbk->pending_pages) { | |
8770b268 | 1075 | rc = -ENOMEM; |
4d05a28d | 1076 | goto out_of_memory; |
8770b268 | 1077 | } |
4d05a28d | 1078 | |
464fb419 | 1079 | for (i = 0; i < mmap_pages; i++) { |
e8e28871 | 1080 | blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE; |
a742b02c | 1081 | blkbk->pending_pages[i] = alloc_page(GFP_KERNEL); |
464fb419 KRW |
1082 | if (blkbk->pending_pages[i] == NULL) { |
1083 | rc = -ENOMEM; | |
1084 | goto out_of_memory; | |
1085 | } | |
1086 | } | |
8b6bf747 | 1087 | rc = xen_blkif_interface_init(); |
8770b268 KRW |
1088 | if (rc) |
1089 | goto failed_init; | |
4d05a28d | 1090 | |
e8e28871 KRW |
1091 | INIT_LIST_HEAD(&blkbk->pending_free); |
1092 | spin_lock_init(&blkbk->pending_free_lock); | |
1093 | init_waitqueue_head(&blkbk->pending_free_wq); | |
4d05a28d | 1094 | |
8b6bf747 | 1095 | for (i = 0; i < xen_blkif_reqs; i++) |
2e9977c2 KRW |
1096 | list_add_tail(&blkbk->pending_reqs[i].free_list, |
1097 | &blkbk->pending_free); | |
4d05a28d | 1098 | |
8b6bf747 | 1099 | rc = xen_blkif_xenbus_init(); |
8770b268 KRW |
1100 | if (rc) |
1101 | goto failed_init; | |
4d05a28d KRW |
1102 | |
1103 | return 0; | |
1104 | ||
1105 | out_of_memory: | |
22b20f2d | 1106 | pr_alert(DRV_PFX "%s: out of memory\n", __func__); |
8770b268 | 1107 | failed_init: |
e8e28871 | 1108 | kfree(blkbk->pending_reqs); |
a742b02c | 1109 | kfree(blkbk->pending_grant_handles); |
9b83c771 DC |
1110 | if (blkbk->pending_pages) { |
1111 | for (i = 0; i < mmap_pages; i++) { | |
1112 | if (blkbk->pending_pages[i]) | |
1113 | __free_page(blkbk->pending_pages[i]); | |
1114 | } | |
1115 | kfree(blkbk->pending_pages); | |
464fb419 | 1116 | } |
a742b02c | 1117 | kfree(blkbk); |
e8e28871 | 1118 | blkbk = NULL; |
8770b268 | 1119 | return rc; |
4d05a28d KRW |
1120 | } |
1121 | ||
8b6bf747 | 1122 | module_init(xen_blkif_init); |
4d05a28d KRW |
1123 | |
1124 | MODULE_LICENSE("Dual BSD/GPL"); | |
a7e9357f | 1125 | MODULE_ALIAS("xen-backend:vbd"); |