]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/block/xen-blkback/blkback.c
xen-blkfront: improve aproximation of required grants per request
[mirror_ubuntu-hirsute-kernel.git] / drivers / block / xen-blkback / blkback.c
CommitLineData
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>
087ffecd 49#include <xen/balloon.h>
4d05a28d
KRW
50#include "common.h"
51
c6cc142d
RPM
52/*
53 * Maximum number of unused free pages to keep in the internal buffer.
54 * Setting this to a value too low will reduce memory used in each backend,
55 * but can have a performance penalty.
56 *
57 * A sane value is xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST, but can
58 * be set to a lower value that might degrade performance on some intensive
59 * IO workloads.
60 */
61
402b27f9 62static int xen_blkif_max_buffer_pages = 1024;
c6cc142d
RPM
63module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
64MODULE_PARM_DESC(max_buffer_pages,
65"Maximum number of free pages to keep in each block backend buffer");
66
3f3aad5e
RPM
67/*
68 * Maximum number of grants to map persistently in blkback. For maximum
69 * performance this should be the total numbers of grants that can be used
70 * to fill the ring, but since this might become too high, specially with
71 * the use of indirect descriptors, we set it to a value that provides good
72 * performance without using too much memory.
73 *
74 * When the list of persistent grants is full we clean it up using a LRU
75 * algorithm.
76 */
77
402b27f9 78static int xen_blkif_max_pgrants = 1056;
3f3aad5e
RPM
79module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
80MODULE_PARM_DESC(max_persistent_grants,
81 "Maximum number of grants to map persistently");
82
83/*
84 * The LRU mechanism to clean the lists of persistent grants needs to
85 * be executed periodically. The time interval between consecutive executions
86 * of the purge mechanism is set in ms.
87 */
88#define LRU_INTERVAL 100
89
90/*
91 * When the persistent grants list is full we will remove unused grants
92 * from the list. The percent number of grants to be removed at each LRU
93 * execution.
94 */
95#define LRU_PERCENT_CLEAN 5
96
4d05a28d 97/* Run-time switchable: /sys/module/blkback/parameters/ */
2e9977c2 98static unsigned int log_stats;
4d05a28d 99module_param(log_stats, int, 0644);
4d05a28d 100
4d05a28d
KRW
101#define BLKBACK_INVALID_HANDLE (~0)
102
c6cc142d
RPM
103/* Number of free pages to remove on each call to free_xenballooned_pages */
104#define NUM_BATCH_FREE_PAGES 10
105
c6cc142d
RPM
106static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
107{
108 unsigned long flags;
109
110 spin_lock_irqsave(&blkif->free_pages_lock, flags);
111 if (list_empty(&blkif->free_pages)) {
112 BUG_ON(blkif->free_pages_num != 0);
113 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
114 return alloc_xenballooned_pages(1, page, false);
115 }
116 BUG_ON(blkif->free_pages_num == 0);
117 page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
118 list_del(&page[0]->lru);
119 blkif->free_pages_num--;
120 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
efe08a3e 121
c6cc142d
RPM
122 return 0;
123}
124
125static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
126 int num)
4d05a28d 127{
c6cc142d
RPM
128 unsigned long flags;
129 int i;
130
131 spin_lock_irqsave(&blkif->free_pages_lock, flags);
132 for (i = 0; i < num; i++)
133 list_add(&page[i]->lru, &blkif->free_pages);
134 blkif->free_pages_num += num;
135 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
136}
137
138static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
139{
140 /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
141 struct page *page[NUM_BATCH_FREE_PAGES];
142 unsigned int num_pages = 0;
143 unsigned long flags;
144
145 spin_lock_irqsave(&blkif->free_pages_lock, flags);
146 while (blkif->free_pages_num > num) {
147 BUG_ON(list_empty(&blkif->free_pages));
148 page[num_pages] = list_first_entry(&blkif->free_pages,
149 struct page, lru);
150 list_del(&page[num_pages]->lru);
151 blkif->free_pages_num--;
152 if (++num_pages == NUM_BATCH_FREE_PAGES) {
153 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
154 free_xenballooned_pages(num_pages, page);
155 spin_lock_irqsave(&blkif->free_pages_lock, flags);
156 num_pages = 0;
157 }
158 }
159 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
160 if (num_pages != 0)
161 free_xenballooned_pages(num_pages, page);
4d05a28d
KRW
162}
163
c6cc142d
RPM
164#define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
165
30fd1502
KRW
166static int do_block_io_op(struct xen_blkif *blkif);
167static int dispatch_rw_block_io(struct xen_blkif *blkif,
fc53bf75
KRW
168 struct blkif_request *req,
169 struct pending_req *pending_req);
30fd1502 170static void make_response(struct xen_blkif *blkif, u64 id,
4d05a28d
KRW
171 unsigned short op, int st);
172
7dc34117
RPM
173#define foreach_grant_safe(pos, n, rbtree, node) \
174 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
217fd5e7 175 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
0a8704a5 176 &(pos)->node != NULL; \
7dc34117
RPM
177 (pos) = container_of(n, typeof(*(pos)), node), \
178 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
0a8704a5
RPM
179
180
3f3aad5e
RPM
181/*
182 * We don't need locking around the persistent grant helpers
183 * because blkback uses a single-thread for each backed, so we
184 * can be sure that this functions will never be called recursively.
185 *
186 * The only exception to that is put_persistent_grant, that can be called
187 * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
188 * bit operations to modify the flags of a persistent grant and to count
189 * the number of used grants.
190 */
191static int add_persistent_gnt(struct xen_blkif *blkif,
0a8704a5
RPM
192 struct persistent_gnt *persistent_gnt)
193{
3f3aad5e 194 struct rb_node **new = NULL, *parent = NULL;
0a8704a5
RPM
195 struct persistent_gnt *this;
196
3f3aad5e
RPM
197 if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
198 if (!blkif->vbd.overflow_max_grants)
199 blkif->vbd.overflow_max_grants = 1;
200 return -EBUSY;
201 }
0a8704a5 202 /* Figure out where to put new node */
3f3aad5e 203 new = &blkif->persistent_gnts.rb_node;
0a8704a5
RPM
204 while (*new) {
205 this = container_of(*new, struct persistent_gnt, node);
206
207 parent = *new;
208 if (persistent_gnt->gnt < this->gnt)
209 new = &((*new)->rb_left);
210 else if (persistent_gnt->gnt > this->gnt)
211 new = &((*new)->rb_right);
212 else {
c6cc142d
RPM
213 pr_alert_ratelimited(DRV_PFX " trying to add a gref that's already in the tree\n");
214 return -EINVAL;
0a8704a5
RPM
215 }
216 }
217
3f3aad5e
RPM
218 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
219 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
0a8704a5
RPM
220 /* Add new node and rebalance tree. */
221 rb_link_node(&(persistent_gnt->node), parent, new);
3f3aad5e
RPM
222 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
223 blkif->persistent_gnt_c++;
224 atomic_inc(&blkif->persistent_gnt_in_use);
c6cc142d 225 return 0;
0a8704a5
RPM
226}
227
3f3aad5e 228static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
0a8704a5
RPM
229 grant_ref_t gref)
230{
231 struct persistent_gnt *data;
3f3aad5e 232 struct rb_node *node = NULL;
0a8704a5 233
3f3aad5e 234 node = blkif->persistent_gnts.rb_node;
0a8704a5
RPM
235 while (node) {
236 data = container_of(node, struct persistent_gnt, node);
237
238 if (gref < data->gnt)
239 node = node->rb_left;
240 else if (gref > data->gnt)
241 node = node->rb_right;
3f3aad5e
RPM
242 else {
243 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
244 pr_alert_ratelimited(DRV_PFX " requesting a grant already in use\n");
245 return NULL;
246 }
247 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
248 atomic_inc(&blkif->persistent_gnt_in_use);
0a8704a5 249 return data;
3f3aad5e 250 }
0a8704a5
RPM
251 }
252 return NULL;
253}
254
3f3aad5e
RPM
255static void put_persistent_gnt(struct xen_blkif *blkif,
256 struct persistent_gnt *persistent_gnt)
257{
258 if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
259 pr_alert_ratelimited(DRV_PFX " freeing a grant already unused");
260 set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
261 clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
262 atomic_dec(&blkif->persistent_gnt_in_use);
263}
264
c6cc142d
RPM
265static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
266 unsigned int num)
4d4f270f
RPM
267{
268 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
269 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
270 struct persistent_gnt *persistent_gnt;
7dc34117 271 struct rb_node *n;
4d4f270f
RPM
272 int ret = 0;
273 int segs_to_unmap = 0;
274
7dc34117 275 foreach_grant_safe(persistent_gnt, n, root, node) {
4d4f270f
RPM
276 BUG_ON(persistent_gnt->handle ==
277 BLKBACK_INVALID_HANDLE);
278 gnttab_set_unmap_op(&unmap[segs_to_unmap],
279 (unsigned long) pfn_to_kaddr(page_to_pfn(
280 persistent_gnt->page)),
281 GNTMAP_host_map,
282 persistent_gnt->handle);
283
284 pages[segs_to_unmap] = persistent_gnt->page;
4d4f270f
RPM
285
286 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
287 !rb_next(&persistent_gnt->node)) {
288 ret = gnttab_unmap_refs(unmap, NULL, pages,
289 segs_to_unmap);
290 BUG_ON(ret);
c6cc142d 291 put_free_pages(blkif, pages, segs_to_unmap);
4d4f270f
RPM
292 segs_to_unmap = 0;
293 }
7dc34117
RPM
294
295 rb_erase(&persistent_gnt->node, root);
296 kfree(persistent_gnt);
297 num--;
4d4f270f
RPM
298 }
299 BUG_ON(num != 0);
300}
301
3f3aad5e
RPM
302static void unmap_purged_grants(struct work_struct *work)
303{
304 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
305 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
306 struct persistent_gnt *persistent_gnt;
307 int ret, segs_to_unmap = 0;
308 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
309
310 while(!list_empty(&blkif->persistent_purge_list)) {
311 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
312 struct persistent_gnt,
313 remove_node);
314 list_del(&persistent_gnt->remove_node);
315
316 gnttab_set_unmap_op(&unmap[segs_to_unmap],
317 vaddr(persistent_gnt->page),
318 GNTMAP_host_map,
319 persistent_gnt->handle);
320
321 pages[segs_to_unmap] = persistent_gnt->page;
322
323 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
324 ret = gnttab_unmap_refs(unmap, NULL, pages,
325 segs_to_unmap);
326 BUG_ON(ret);
327 put_free_pages(blkif, pages, segs_to_unmap);
328 segs_to_unmap = 0;
329 }
330 kfree(persistent_gnt);
331 }
332 if (segs_to_unmap > 0) {
333 ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
334 BUG_ON(ret);
335 put_free_pages(blkif, pages, segs_to_unmap);
336 }
337}
338
339static void purge_persistent_gnt(struct xen_blkif *blkif)
340{
341 struct persistent_gnt *persistent_gnt;
342 struct rb_node *n;
343 unsigned int num_clean, total;
2d910543 344 bool scan_used = false, clean_used = false;
3f3aad5e
RPM
345 struct rb_root *root;
346
347 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
348 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
349 !blkif->vbd.overflow_max_grants)) {
350 return;
351 }
352
353 if (work_pending(&blkif->persistent_purge_work)) {
354 pr_alert_ratelimited(DRV_PFX "Scheduled work from previous purge is still pending, cannot purge list\n");
355 return;
356 }
357
358 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
359 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
360 num_clean = min(blkif->persistent_gnt_c, num_clean);
2d910543
RPM
361 if ((num_clean == 0) ||
362 (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
3f3aad5e
RPM
363 return;
364
365 /*
366 * At this point, we can assure that there will be no calls
367 * to get_persistent_grant (because we are executing this code from
368 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
369 * which means that the number of currently used grants will go down,
370 * but never up, so we will always be able to remove the requested
371 * number of grants.
372 */
373
374 total = num_clean;
375
376 pr_debug(DRV_PFX "Going to purge %u persistent grants\n", num_clean);
377
378 INIT_LIST_HEAD(&blkif->persistent_purge_list);
379 root = &blkif->persistent_gnts;
380purge_list:
381 foreach_grant_safe(persistent_gnt, n, root, node) {
382 BUG_ON(persistent_gnt->handle ==
383 BLKBACK_INVALID_HANDLE);
384
2d910543
RPM
385 if (clean_used) {
386 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
387 continue;
388 }
389
3f3aad5e
RPM
390 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
391 continue;
392 if (!scan_used &&
393 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
394 continue;
395
396 rb_erase(&persistent_gnt->node, root);
397 list_add(&persistent_gnt->remove_node,
398 &blkif->persistent_purge_list);
399 if (--num_clean == 0)
400 goto finished;
401 }
402 /*
403 * If we get here it means we also need to start cleaning
404 * grants that were used since last purge in order to cope
405 * with the requested num
406 */
2d910543 407 if (!scan_used && !clean_used) {
3f3aad5e
RPM
408 pr_debug(DRV_PFX "Still missing %u purged frames\n", num_clean);
409 scan_used = true;
410 goto purge_list;
411 }
412finished:
2d910543
RPM
413 if (!clean_used) {
414 pr_debug(DRV_PFX "Finished scanning for grants to clean, removing used flag\n");
415 clean_used = true;
416 goto purge_list;
3f3aad5e 417 }
2d910543 418
3f3aad5e
RPM
419 blkif->persistent_gnt_c -= (total - num_clean);
420 blkif->vbd.overflow_max_grants = 0;
421
422 /* We can defer this work */
423 INIT_WORK(&blkif->persistent_purge_work, unmap_purged_grants);
424 schedule_work(&blkif->persistent_purge_work);
425 pr_debug(DRV_PFX "Purged %u/%u\n", (total - num_clean), total);
426 return;
427}
428
a1397fa3
KRW
429/*
430 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
4d05a28d 431 */
bf0720c4 432static struct pending_req *alloc_req(struct xen_blkif *blkif)
4d05a28d 433{
2e9977c2 434 struct pending_req *req = NULL;
4d05a28d
KRW
435 unsigned long flags;
436
bf0720c4
RPM
437 spin_lock_irqsave(&blkif->pending_free_lock, flags);
438 if (!list_empty(&blkif->pending_free)) {
439 req = list_entry(blkif->pending_free.next, struct pending_req,
2e9977c2 440 free_list);
4d05a28d
KRW
441 list_del(&req->free_list);
442 }
bf0720c4 443 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
4d05a28d
KRW
444 return req;
445}
446
a1397fa3
KRW
447/*
448 * Return the 'pending_req' structure back to the freepool. We also
449 * wake up the thread if it was waiting for a free page.
450 */
bf0720c4 451static void free_req(struct xen_blkif *blkif, struct pending_req *req)
4d05a28d
KRW
452{
453 unsigned long flags;
454 int was_empty;
455
bf0720c4
RPM
456 spin_lock_irqsave(&blkif->pending_free_lock, flags);
457 was_empty = list_empty(&blkif->pending_free);
458 list_add(&req->free_list, &blkif->pending_free);
459 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
4d05a28d 460 if (was_empty)
bf0720c4 461 wake_up(&blkif->pending_free_wq);
4d05a28d
KRW
462}
463
ee9ff853
KRW
464/*
465 * Routines for managing virtual block devices (vbds).
466 */
3d814731
KRW
467static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
468 int operation)
ee9ff853 469{
3d814731 470 struct xen_vbd *vbd = &blkif->vbd;
ee9ff853
KRW
471 int rc = -EACCES;
472
473 if ((operation != READ) && vbd->readonly)
474 goto out;
475
8ab52150
JB
476 if (likely(req->nr_sects)) {
477 blkif_sector_t end = req->sector_number + req->nr_sects;
478
479 if (unlikely(end < req->sector_number))
480 goto out;
481 if (unlikely(end > vbd_sz(vbd)))
482 goto out;
483 }
ee9ff853
KRW
484
485 req->dev = vbd->pdevice;
486 req->bdev = vbd->bdev;
487 rc = 0;
488
489 out:
490 return rc;
491}
492
3d814731 493static void xen_vbd_resize(struct xen_blkif *blkif)
ee9ff853 494{
3d814731 495 struct xen_vbd *vbd = &blkif->vbd;
ee9ff853
KRW
496 struct xenbus_transaction xbt;
497 int err;
8b6bf747 498 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
42c7841d 499 unsigned long long new_size = vbd_sz(vbd);
ee9ff853 500
22b20f2d 501 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
ee9ff853 502 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
22b20f2d 503 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
ee9ff853
KRW
504 vbd->size = new_size;
505again:
506 err = xenbus_transaction_start(&xbt);
507 if (err) {
22b20f2d 508 pr_warn(DRV_PFX "Error starting transaction");
ee9ff853
KRW
509 return;
510 }
511 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
42c7841d 512 (unsigned long long)vbd_sz(vbd));
ee9ff853 513 if (err) {
22b20f2d 514 pr_warn(DRV_PFX "Error writing new size");
ee9ff853
KRW
515 goto abort;
516 }
517 /*
518 * Write the current state; we will use this to synchronize
519 * the front-end. If the current state is "connected" the
520 * front-end will get the new size information online.
521 */
522 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
523 if (err) {
22b20f2d 524 pr_warn(DRV_PFX "Error writing the state");
ee9ff853
KRW
525 goto abort;
526 }
527
528 err = xenbus_transaction_end(xbt, 0);
529 if (err == -EAGAIN)
530 goto again;
531 if (err)
22b20f2d 532 pr_warn(DRV_PFX "Error ending transaction");
496b318e 533 return;
ee9ff853
KRW
534abort:
535 xenbus_transaction_end(xbt, 1);
536}
537
a1397fa3 538/*
b0aef179
KRW
539 * Notification from the guest OS.
540 */
30fd1502 541static void blkif_notify_work(struct xen_blkif *blkif)
4d05a28d 542{
b0aef179
KRW
543 blkif->waiting_reqs = 1;
544 wake_up(&blkif->wq);
545}
4d05a28d 546
8b6bf747 547irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
b0aef179
KRW
548{
549 blkif_notify_work(dev_id);
550 return IRQ_HANDLED;
4d05a28d
KRW
551}
552
2e9977c2 553/*
4d05a28d
KRW
554 * SCHEDULER FUNCTIONS
555 */
556
30fd1502 557static void print_stats(struct xen_blkif *blkif)
4d05a28d 558{
986cacbd 559 pr_info("xen-blkback (%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
3f3aad5e 560 " | ds %4llu | pg: %4u/%4d\n",
ebe81906 561 current->comm, blkif->st_oo_req,
b3cb0d6a 562 blkif->st_rd_req, blkif->st_wr_req,
c1a15d08
RPM
563 blkif->st_f_req, blkif->st_ds_req,
564 blkif->persistent_gnt_c,
3f3aad5e 565 xen_blkif_max_pgrants);
4d05a28d
KRW
566 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
567 blkif->st_rd_req = 0;
568 blkif->st_wr_req = 0;
569 blkif->st_oo_req = 0;
b3cb0d6a 570 blkif->st_ds_req = 0;
4d05a28d
KRW
571}
572
8b6bf747 573int xen_blkif_schedule(void *arg)
4d05a28d 574{
30fd1502 575 struct xen_blkif *blkif = arg;
3d814731 576 struct xen_vbd *vbd = &blkif->vbd;
3f3aad5e 577 unsigned long timeout;
8e3f8755 578 int ret;
4d05a28d 579
8b6bf747 580 xen_blkif_get(blkif);
4d05a28d 581
4d05a28d
KRW
582 while (!kthread_should_stop()) {
583 if (try_to_freeze())
584 continue;
42c7841d 585 if (unlikely(vbd->size != vbd_sz(vbd)))
3d814731 586 xen_vbd_resize(blkif);
4d05a28d 587
3f3aad5e
RPM
588 timeout = msecs_to_jiffies(LRU_INTERVAL);
589
590 timeout = wait_event_interruptible_timeout(
4d05a28d 591 blkif->wq,
3f3aad5e
RPM
592 blkif->waiting_reqs || kthread_should_stop(),
593 timeout);
594 if (timeout == 0)
595 goto purge_gnt_list;
596 timeout = wait_event_interruptible_timeout(
bf0720c4
RPM
597 blkif->pending_free_wq,
598 !list_empty(&blkif->pending_free) ||
3f3aad5e
RPM
599 kthread_should_stop(),
600 timeout);
601 if (timeout == 0)
602 goto purge_gnt_list;
4d05a28d
KRW
603
604 blkif->waiting_reqs = 0;
605 smp_mb(); /* clear flag *before* checking for work */
606
8e3f8755
KRW
607 ret = do_block_io_op(blkif);
608 if (ret > 0)
4d05a28d 609 blkif->waiting_reqs = 1;
8e3f8755
KRW
610 if (ret == -EACCES)
611 wait_event_interruptible(blkif->shutdown_wq,
612 kthread_should_stop());
4d05a28d 613
3f3aad5e
RPM
614purge_gnt_list:
615 if (blkif->vbd.feature_gnt_persistent &&
616 time_after(jiffies, blkif->next_lru)) {
617 purge_persistent_gnt(blkif);
618 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
619 }
620
c6cc142d
RPM
621 /* Shrink if we have more than xen_blkif_max_buffer_pages */
622 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
623
4d05a28d
KRW
624 if (log_stats && time_after(jiffies, blkif->st_print))
625 print_stats(blkif);
626 }
627
c6cc142d
RPM
628 /* Since we are shutting down remove all pages from the buffer */
629 shrink_free_pagepool(blkif, 0 /* All */);
630
0a8704a5 631 /* Free all persistent grant pages */
4d4f270f 632 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
c6cc142d 633 free_persistent_gnts(blkif, &blkif->persistent_gnts,
4d4f270f 634 blkif->persistent_gnt_c);
0a8704a5 635
0a8704a5 636 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
4d4f270f 637 blkif->persistent_gnt_c = 0;
0a8704a5 638
4d05a28d
KRW
639 if (log_stats)
640 print_stats(blkif);
4d05a28d
KRW
641
642 blkif->xenblkd = NULL;
8b6bf747 643 xen_blkif_put(blkif);
4d05a28d
KRW
644
645 return 0;
646}
647
b0aef179
KRW
648/*
649 * Unmap the grant references, and also remove the M2P over-rides
650 * used in the 'pending_req'.
01f37f2d 651 */
31552ee3 652static void xen_blkbk_unmap(struct xen_blkif *blkif,
bb642e83 653 struct grant_page *pages[],
31552ee3 654 int num)
b0aef179
KRW
655{
656 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
31552ee3 657 struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
b0aef179 658 unsigned int i, invcount = 0;
b0aef179
KRW
659 int ret;
660
31552ee3 661 for (i = 0; i < num; i++) {
bb642e83
RPM
662 if (pages[i]->persistent_gnt != NULL) {
663 put_persistent_gnt(blkif, pages[i]->persistent_gnt);
0a8704a5 664 continue;
3f3aad5e 665 }
bb642e83 666 if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
b0aef179 667 continue;
bb642e83
RPM
668 unmap_pages[invcount] = pages[i]->page;
669 gnttab_set_unmap_op(&unmap[invcount], vaddr(pages[i]->page),
670 GNTMAP_host_map, pages[i]->handle);
671 pages[i]->handle = BLKBACK_INVALID_HANDLE;
31552ee3
RPM
672 if (++invcount == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
673 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages,
674 invcount);
675 BUG_ON(ret);
676 put_free_pages(blkif, unmap_pages, invcount);
677 invcount = 0;
678 }
679 }
680 if (invcount) {
681 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
682 BUG_ON(ret);
683 put_free_pages(blkif, unmap_pages, invcount);
b0aef179 684 }
b0aef179 685}
01f37f2d 686
bb642e83
RPM
687static int xen_blkbk_map(struct xen_blkif *blkif,
688 struct grant_page *pages[],
31552ee3 689 int num, bool ro)
1a95fe6e
KRW
690{
691 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
0a8704a5
RPM
692 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
693 struct persistent_gnt *persistent_gnt = NULL;
0a8704a5 694 phys_addr_t addr = 0;
c6cc142d 695 int i, seg_idx, new_map_idx;
0a8704a5 696 int segs_to_map = 0;
1a95fe6e 697 int ret = 0;
31552ee3 698 int last_map = 0, map_until = 0;
0a8704a5
RPM
699 int use_persistent_gnts;
700
701 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
702
01f37f2d
KRW
703 /*
704 * Fill out preq.nr_sects with proper amount of sectors, and setup
1a95fe6e
KRW
705 * assign map[..] with the PFN of the page in our domain with the
706 * corresponding grant reference for each page.
707 */
31552ee3
RPM
708again:
709 for (i = map_until; i < num; i++) {
1a95fe6e
KRW
710 uint32_t flags;
711
0a8704a5
RPM
712 if (use_persistent_gnts)
713 persistent_gnt = get_persistent_gnt(
3f3aad5e 714 blkif,
bb642e83 715 pages[i]->gref);
0a8704a5
RPM
716
717 if (persistent_gnt) {
718 /*
719 * We are using persistent grants and
720 * the grant is already mapped
721 */
bb642e83
RPM
722 pages[i]->page = persistent_gnt->page;
723 pages[i]->persistent_gnt = persistent_gnt;
0a8704a5 724 } else {
bb642e83 725 if (get_free_page(blkif, &pages[i]->page))
c6cc142d 726 goto out_of_memory;
bb642e83
RPM
727 addr = vaddr(pages[i]->page);
728 pages_to_gnt[segs_to_map] = pages[i]->page;
729 pages[i]->persistent_gnt = NULL;
0a8704a5 730 flags = GNTMAP_host_map;
31552ee3 731 if (!use_persistent_gnts && ro)
0a8704a5
RPM
732 flags |= GNTMAP_readonly;
733 gnttab_set_map_op(&map[segs_to_map++], addr,
bb642e83 734 flags, pages[i]->gref,
0a8704a5
RPM
735 blkif->domid);
736 }
31552ee3
RPM
737 map_until = i + 1;
738 if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
739 break;
1a95fe6e
KRW
740 }
741
0a8704a5
RPM
742 if (segs_to_map) {
743 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
744 BUG_ON(ret);
745 }
1a95fe6e 746
01f37f2d
KRW
747 /*
748 * Now swizzle the MFN in our domain with the MFN from the other domain
1a95fe6e
KRW
749 * so that when we access vaddr(pending_req,i) it has the contents of
750 * the page from the other domain.
751 */
31552ee3 752 for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
bb642e83 753 if (!pages[seg_idx]->persistent_gnt) {
0a8704a5 754 /* This is a newly mapped grant */
c6cc142d
RPM
755 BUG_ON(new_map_idx >= segs_to_map);
756 if (unlikely(map[new_map_idx].status != 0)) {
0a8704a5 757 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
bb642e83 758 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
0a8704a5 759 ret |= 1;
31552ee3 760 goto next;
0a8704a5 761 }
bb642e83 762 pages[seg_idx]->handle = map[new_map_idx].handle;
c6cc142d 763 } else {
31552ee3 764 continue;
0a8704a5 765 }
c6cc142d 766 if (use_persistent_gnts &&
3f3aad5e 767 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
c6cc142d
RPM
768 /*
769 * We are using persistent grants, the grant is
3f3aad5e 770 * not mapped but we might have room for it.
c6cc142d
RPM
771 */
772 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
773 GFP_KERNEL);
774 if (!persistent_gnt) {
0a8704a5 775 /*
c6cc142d
RPM
776 * If we don't have enough memory to
777 * allocate the persistent_gnt struct
778 * map this grant non-persistenly
0a8704a5 779 */
31552ee3 780 goto next;
0a8704a5 781 }
c6cc142d
RPM
782 persistent_gnt->gnt = map[new_map_idx].ref;
783 persistent_gnt->handle = map[new_map_idx].handle;
bb642e83 784 persistent_gnt->page = pages[seg_idx]->page;
3f3aad5e 785 if (add_persistent_gnt(blkif,
c6cc142d
RPM
786 persistent_gnt)) {
787 kfree(persistent_gnt);
788 persistent_gnt = NULL;
31552ee3 789 goto next;
c6cc142d 790 }
bb642e83 791 pages[seg_idx]->persistent_gnt = persistent_gnt;
c6cc142d
RPM
792 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
793 persistent_gnt->gnt, blkif->persistent_gnt_c,
3f3aad5e 794 xen_blkif_max_pgrants);
c6cc142d
RPM
795 goto next;
796 }
797 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
798 blkif->vbd.overflow_max_grants = 1;
799 pr_debug(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
800 blkif->domid, blkif->vbd.handle);
1a95fe6e 801 }
c6cc142d
RPM
802 /*
803 * We could not map this grant persistently, so use it as
804 * a non-persistent grant.
805 */
c6cc142d 806next:
31552ee3 807 new_map_idx++;
1a95fe6e 808 }
31552ee3
RPM
809 segs_to_map = 0;
810 last_map = map_until;
811 if (map_until != num)
812 goto again;
813
1a95fe6e 814 return ret;
c6cc142d
RPM
815
816out_of_memory:
817 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
818 put_free_pages(blkif, pages_to_gnt, segs_to_map);
819 return -ENOMEM;
1a95fe6e
KRW
820}
821
bb642e83 822static int xen_blkbk_map_seg(struct pending_req *pending_req)
31552ee3 823{
402b27f9 824 int rc;
31552ee3 825
bb642e83 826 rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
402b27f9 827 pending_req->nr_pages,
31552ee3 828 (pending_req->operation != BLKIF_OP_READ));
31552ee3 829
402b27f9
RPM
830 return rc;
831}
31552ee3 832
402b27f9
RPM
833static int xen_blkbk_parse_indirect(struct blkif_request *req,
834 struct pending_req *pending_req,
835 struct seg_buf seg[],
836 struct phys_req *preq)
837{
bb642e83 838 struct grant_page **pages = pending_req->indirect_pages;
402b27f9
RPM
839 struct xen_blkif *blkif = pending_req->blkif;
840 int indirect_grefs, rc, n, nseg, i;
841 struct blkif_request_segment_aligned *segments = NULL;
842
843 nseg = pending_req->nr_pages;
844 indirect_grefs = INDIRECT_PAGES(nseg);
845 BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
846
bb642e83
RPM
847 for (i = 0; i < indirect_grefs; i++)
848 pages[i]->gref = req->u.indirect.indirect_grefs[i];
849
850 rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
402b27f9
RPM
851 if (rc)
852 goto unmap;
853
854 for (n = 0, i = 0; n < nseg; n++) {
855 if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
856 /* Map indirect segments */
857 if (segments)
858 kunmap_atomic(segments);
bb642e83 859 segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
402b27f9
RPM
860 }
861 i = n % SEGS_PER_INDIRECT_FRAME;
bb642e83 862 pending_req->segments[n]->gref = segments[i].gref;
402b27f9
RPM
863 seg[n].nsec = segments[i].last_sect -
864 segments[i].first_sect + 1;
865 seg[n].offset = (segments[i].first_sect << 9);
866 if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
867 (segments[i].last_sect < segments[i].first_sect)) {
868 rc = -EINVAL;
869 goto unmap;
870 }
871 preq->nr_sects += seg[n].nsec;
872 }
873
874unmap:
875 if (segments)
876 kunmap_atomic(segments);
bb642e83 877 xen_blkbk_unmap(blkif, pages, indirect_grefs);
402b27f9 878 return rc;
31552ee3
RPM
879}
880
42146352
KRW
881static int dispatch_discard_io(struct xen_blkif *blkif,
882 struct blkif_request *req)
b3cb0d6a
LD
883{
884 int err = 0;
885 int status = BLKIF_RSP_OKAY;
886 struct block_device *bdev = blkif->vbd.bdev;
4dae7670 887 unsigned long secure;
604c499c 888 struct phys_req preq;
b3cb0d6a 889
604c499c
KRW
890 preq.sector_number = req->u.discard.sector_number;
891 preq.nr_sects = req->u.discard.nr_sectors;
892
893 err = xen_vbd_translate(&preq, blkif, WRITE);
894 if (err) {
895 pr_warn(DRV_PFX "access denied: DISCARD [%llu->%llu] on dev=%04x\n",
896 preq.sector_number,
897 preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
898 goto fail_response;
899 }
42146352
KRW
900 blkif->st_ds_req++;
901
902 xen_blkif_get(blkif);
4dae7670
KRW
903 secure = (blkif->vbd.discard_secure &&
904 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
905 BLKDEV_DISCARD_SECURE : 0;
906
907 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
908 req->u.discard.nr_sectors,
909 GFP_KERNEL, secure);
604c499c 910fail_response:
b3cb0d6a
LD
911 if (err == -EOPNOTSUPP) {
912 pr_debug(DRV_PFX "discard op failed, not supported\n");
913 status = BLKIF_RSP_EOPNOTSUPP;
914 } else if (err)
915 status = BLKIF_RSP_ERROR;
916
97e36834 917 make_response(blkif, req->u.discard.id, req->operation, status);
42146352
KRW
918 xen_blkif_put(blkif);
919 return err;
b3cb0d6a
LD
920}
921
0e367ae4
DV
922static int dispatch_other_io(struct xen_blkif *blkif,
923 struct blkif_request *req,
924 struct pending_req *pending_req)
925{
bf0720c4 926 free_req(blkif, pending_req);
0e367ae4
DV
927 make_response(blkif, req->u.other.id, req->operation,
928 BLKIF_RSP_EOPNOTSUPP);
929 return -EIO;
930}
931
29bde093
KRW
932static void xen_blk_drain_io(struct xen_blkif *blkif)
933{
934 atomic_set(&blkif->drain, 1);
935 do {
6927d920
KRW
936 /* The initial value is one, and one refcnt taken at the
937 * start of the xen_blkif_schedule thread. */
938 if (atomic_read(&blkif->refcnt) <= 2)
939 break;
29bde093
KRW
940 wait_for_completion_interruptible_timeout(
941 &blkif->drain_complete, HZ);
942
943 if (!atomic_read(&blkif->drain))
944 break;
29bde093
KRW
945 } while (!kthread_should_stop());
946 atomic_set(&blkif->drain, 0);
947}
948
a1397fa3
KRW
949/*
950 * Completion callback on the bio's. Called as bh->b_end_io()
4d05a28d
KRW
951 */
952
2e9977c2 953static void __end_block_io_op(struct pending_req *pending_req, int error)
4d05a28d
KRW
954{
955 /* An error fails the entire request. */
24f567f9 956 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
4d05a28d 957 (error == -EOPNOTSUPP)) {
22b20f2d 958 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
24f567f9 959 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
4d05a28d 960 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
29bde093
KRW
961 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
962 (error == -EOPNOTSUPP)) {
963 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
964 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
965 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
4d05a28d 966 } else if (error) {
22b20f2d 967 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
ebe81906 968 " error=%d\n", error);
4d05a28d
KRW
969 pending_req->status = BLKIF_RSP_ERROR;
970 }
971
01f37f2d
KRW
972 /*
973 * If all of the bio's have completed it is time to unmap
a1397fa3 974 * the grant references associated with 'request' and provide
2e9977c2
KRW
975 * the proper response on the ring.
976 */
4d05a28d 977 if (atomic_dec_and_test(&pending_req->pendcnt)) {
bb642e83
RPM
978 xen_blkbk_unmap(pending_req->blkif,
979 pending_req->segments,
31552ee3 980 pending_req->nr_pages);
4d05a28d
KRW
981 make_response(pending_req->blkif, pending_req->id,
982 pending_req->operation, pending_req->status);
8b6bf747 983 xen_blkif_put(pending_req->blkif);
29bde093
KRW
984 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
985 if (atomic_read(&pending_req->blkif->drain))
986 complete(&pending_req->blkif->drain_complete);
987 }
bf0720c4 988 free_req(pending_req->blkif, pending_req);
4d05a28d
KRW
989 }
990}
991
a1397fa3
KRW
992/*
993 * bio callback.
994 */
88122933 995static void end_block_io_op(struct bio *bio, int error)
4d05a28d 996{
4d05a28d
KRW
997 __end_block_io_op(bio->bi_private, error);
998 bio_put(bio);
4d05a28d
KRW
999}
1000
1001
4d05a28d 1002
a1397fa3
KRW
1003/*
1004 * Function to copy the from the ring buffer the 'struct blkif_request'
1005 * (which has the sectors we want, number of them, grant references, etc),
1006 * and transmute it to the block API to hand it over to the proper block disk.
4d05a28d 1007 */
b4726a9d
DS
1008static int
1009__do_block_io_op(struct xen_blkif *blkif)
4d05a28d 1010{
88122933
JF
1011 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1012 struct blkif_request req;
2e9977c2 1013 struct pending_req *pending_req;
4d05a28d
KRW
1014 RING_IDX rc, rp;
1015 int more_to_do = 0;
1016
1017 rc = blk_rings->common.req_cons;
1018 rp = blk_rings->common.sring->req_prod;
1019 rmb(); /* Ensure we see queued requests up to 'rp'. */
1020
8e3f8755
KRW
1021 if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
1022 rc = blk_rings->common.rsp_prod_pvt;
1023 pr_warn(DRV_PFX "Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
1024 rp, rc, rp - rc, blkif->vbd.pdevice);
1025 return -EACCES;
1026 }
4d05a28d
KRW
1027 while (rc != rp) {
1028
1029 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1030 break;
1031
8270b45b 1032 if (kthread_should_stop()) {
4d05a28d
KRW
1033 more_to_do = 1;
1034 break;
1035 }
1036
bf0720c4 1037 pending_req = alloc_req(blkif);
8270b45b
KF
1038 if (NULL == pending_req) {
1039 blkif->st_oo_req++;
4d05a28d
KRW
1040 more_to_do = 1;
1041 break;
1042 }
1043
1044 switch (blkif->blk_protocol) {
1045 case BLKIF_PROTOCOL_NATIVE:
1046 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1047 break;
1048 case BLKIF_PROTOCOL_X86_32:
1049 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1050 break;
1051 case BLKIF_PROTOCOL_X86_64:
1052 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1053 break;
1054 default:
1055 BUG();
1056 }
1057 blk_rings->common.req_cons = ++rc; /* before make_response() */
1058
1059 /* Apply all sanity checks to /private copy/ of request. */
1060 barrier();
0e367ae4
DV
1061
1062 switch (req.operation) {
1063 case BLKIF_OP_READ:
1064 case BLKIF_OP_WRITE:
1065 case BLKIF_OP_WRITE_BARRIER:
1066 case BLKIF_OP_FLUSH_DISKCACHE:
402b27f9 1067 case BLKIF_OP_INDIRECT:
0e367ae4
DV
1068 if (dispatch_rw_block_io(blkif, &req, pending_req))
1069 goto done;
1070 break;
1071 case BLKIF_OP_DISCARD:
bf0720c4 1072 free_req(blkif, pending_req);
42146352 1073 if (dispatch_discard_io(blkif, &req))
0e367ae4 1074 goto done;
4d05a28d 1075 break;
0e367ae4
DV
1076 default:
1077 if (dispatch_other_io(blkif, &req, pending_req))
1078 goto done;
1079 break;
1080 }
4d05a28d
KRW
1081
1082 /* Yield point for this unbounded loop. */
1083 cond_resched();
1084 }
0e367ae4 1085done:
4d05a28d
KRW
1086 return more_to_do;
1087}
1088
b4726a9d
DS
1089static int
1090do_block_io_op(struct xen_blkif *blkif)
1091{
1092 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1093 int more_to_do;
1094
1095 do {
1096 more_to_do = __do_block_io_op(blkif);
1097 if (more_to_do)
1098 break;
1099
1100 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1101 } while (more_to_do);
1102
1103 return more_to_do;
1104}
a1397fa3 1105/*
01f37f2d
KRW
1106 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1107 * and call the 'submit_bio' to pass it to the underlying storage.
a1397fa3 1108 */
30fd1502
KRW
1109static int dispatch_rw_block_io(struct xen_blkif *blkif,
1110 struct blkif_request *req,
1111 struct pending_req *pending_req)
4d05a28d 1112{
4d05a28d 1113 struct phys_req preq;
402b27f9 1114 struct seg_buf *seg = pending_req->seg;
4d05a28d
KRW
1115 unsigned int nseg;
1116 struct bio *bio = NULL;
402b27f9 1117 struct bio **biolist = pending_req->biolist;
1a95fe6e 1118 int i, nbio = 0;
4d05a28d 1119 int operation;
a19be5f0 1120 struct blk_plug plug;
29bde093 1121 bool drain = false;
bb642e83 1122 struct grant_page **pages = pending_req->segments;
402b27f9
RPM
1123 unsigned short req_operation;
1124
1125 req_operation = req->operation == BLKIF_OP_INDIRECT ?
1126 req->u.indirect.indirect_op : req->operation;
1127 if ((req->operation == BLKIF_OP_INDIRECT) &&
1128 (req_operation != BLKIF_OP_READ) &&
1129 (req_operation != BLKIF_OP_WRITE)) {
1130 pr_debug(DRV_PFX "Invalid indirect operation (%u)\n",
1131 req_operation);
1132 goto fail_response;
1133 }
4d05a28d 1134
402b27f9 1135 switch (req_operation) {
4d05a28d 1136 case BLKIF_OP_READ:
fc53bf75 1137 blkif->st_rd_req++;
4d05a28d
KRW
1138 operation = READ;
1139 break;
1140 case BLKIF_OP_WRITE:
fc53bf75 1141 blkif->st_wr_req++;
013c3ca1 1142 operation = WRITE_ODIRECT;
4d05a28d 1143 break;
29bde093
KRW
1144 case BLKIF_OP_WRITE_BARRIER:
1145 drain = true;
24f567f9 1146 case BLKIF_OP_FLUSH_DISKCACHE:
fc53bf75 1147 blkif->st_f_req++;
24f567f9 1148 operation = WRITE_FLUSH;
4d05a28d
KRW
1149 break;
1150 default:
1151 operation = 0; /* make gcc happy */
fc53bf75
KRW
1152 goto fail_response;
1153 break;
4d05a28d
KRW
1154 }
1155
42146352 1156 /* Check that the number of segments is sane. */
402b27f9
RPM
1157 nseg = req->operation == BLKIF_OP_INDIRECT ?
1158 req->u.indirect.nr_segments : req->u.rw.nr_segments;
97e36834 1159
42146352 1160 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
402b27f9
RPM
1161 unlikely((req->operation != BLKIF_OP_INDIRECT) &&
1162 (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
1163 unlikely((req->operation == BLKIF_OP_INDIRECT) &&
1164 (nseg > MAX_INDIRECT_SEGMENTS))) {
22b20f2d 1165 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
ebe81906 1166 nseg);
1a95fe6e 1167 /* Haven't submitted any bio's yet. */
4d05a28d
KRW
1168 goto fail_response;
1169 }
1170
4d05a28d
KRW
1171 preq.nr_sects = 0;
1172
1173 pending_req->blkif = blkif;
97e36834 1174 pending_req->id = req->u.rw.id;
402b27f9 1175 pending_req->operation = req_operation;
4d05a28d
KRW
1176 pending_req->status = BLKIF_RSP_OKAY;
1177 pending_req->nr_pages = nseg;
e9350493 1178
402b27f9
RPM
1179 if (req->operation != BLKIF_OP_INDIRECT) {
1180 preq.dev = req->u.rw.handle;
1181 preq.sector_number = req->u.rw.sector_number;
1182 for (i = 0; i < nseg; i++) {
bb642e83 1183 pages[i]->gref = req->u.rw.seg[i].gref;
402b27f9
RPM
1184 seg[i].nsec = req->u.rw.seg[i].last_sect -
1185 req->u.rw.seg[i].first_sect + 1;
1186 seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
1187 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1188 (req->u.rw.seg[i].last_sect <
1189 req->u.rw.seg[i].first_sect))
1190 goto fail_response;
1191 preq.nr_sects += seg[i].nsec;
1192 }
1193 } else {
1194 preq.dev = req->u.indirect.handle;
1195 preq.sector_number = req->u.indirect.sector_number;
1196 if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
4d05a28d 1197 goto fail_response;
4d05a28d
KRW
1198 }
1199
3d814731 1200 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
22b20f2d 1201 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
ebe81906
KRW
1202 operation == READ ? "read" : "write",
1203 preq.sector_number,
a72d9002
CG
1204 preq.sector_number + preq.nr_sects,
1205 blkif->vbd.pdevice);
1a95fe6e 1206 goto fail_response;
4d05a28d 1207 }
01f37f2d
KRW
1208
1209 /*
3d814731 1210 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
01f37f2d
KRW
1211 * is set there.
1212 */
e9350493
KRW
1213 for (i = 0; i < nseg; i++) {
1214 if (((int)preq.sector_number|(int)seg[i].nsec) &
1215 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
22b20f2d 1216 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
ebe81906 1217 blkif->domid);
e9350493
KRW
1218 goto fail_response;
1219 }
1220 }
01f37f2d 1221
29bde093
KRW
1222 /* Wait on all outstanding I/O's and once that has been completed
1223 * issue the WRITE_FLUSH.
1224 */
1225 if (drain)
1226 xen_blk_drain_io(pending_req->blkif);
1227
01f37f2d
KRW
1228 /*
1229 * If we have failed at this point, we need to undo the M2P override,
2e9977c2
KRW
1230 * set gnttab_set_unmap_op on all of the grant references and perform
1231 * the hypercall to unmap the grants - that is all done in
9f3aedf5 1232 * xen_blkbk_unmap.
2e9977c2 1233 */
bb642e83 1234 if (xen_blkbk_map_seg(pending_req))
4d05a28d
KRW
1235 goto fail_flush;
1236
b3cb0d6a
LD
1237 /*
1238 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1239 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1240 */
8b6bf747 1241 xen_blkif_get(blkif);
4d05a28d
KRW
1242
1243 for (i = 0; i < nseg; i++) {
4d05a28d
KRW
1244 while ((bio == NULL) ||
1245 (bio_add_page(bio,
bb642e83 1246 pages[i]->page,
4d05a28d 1247 seg[i].nsec << 9,
ffb1dabd 1248 seg[i].offset) == 0)) {
2e9977c2 1249
1e0f7a21
RPM
1250 int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1251 bio = bio_alloc(GFP_KERNEL, nr_iovecs);
4d05a28d
KRW
1252 if (unlikely(bio == NULL))
1253 goto fail_put_bio;
1254
03e0edf9 1255 biolist[nbio++] = bio;
4d05a28d
KRW
1256 bio->bi_bdev = preq.bdev;
1257 bio->bi_private = pending_req;
1258 bio->bi_end_io = end_block_io_op;
1259 bio->bi_sector = preq.sector_number;
1260 }
1261
1262 preq.sector_number += seg[i].nsec;
1263 }
1264
b3cb0d6a 1265 /* This will be hit if the operation was a flush or discard. */
4d05a28d 1266 if (!bio) {
42146352 1267 BUG_ON(operation != WRITE_FLUSH);
b0f80127 1268
42146352
KRW
1269 bio = bio_alloc(GFP_KERNEL, 0);
1270 if (unlikely(bio == NULL))
1271 goto fail_put_bio;
4d05a28d 1272
42146352
KRW
1273 biolist[nbio++] = bio;
1274 bio->bi_bdev = preq.bdev;
1275 bio->bi_private = pending_req;
1276 bio->bi_end_io = end_block_io_op;
4d05a28d
KRW
1277 }
1278
77089926 1279 atomic_set(&pending_req->pendcnt, nbio);
a19be5f0
KRW
1280 blk_start_plug(&plug);
1281
77089926
KRW
1282 for (i = 0; i < nbio; i++)
1283 submit_bio(operation, biolist[i]);
1284
a19be5f0 1285 /* Let the I/Os go.. */
3d68b399 1286 blk_finish_plug(&plug);
a19be5f0 1287
4d05a28d
KRW
1288 if (operation == READ)
1289 blkif->st_rd_sect += preq.nr_sects;
5c62cb48 1290 else if (operation & WRITE)
4d05a28d
KRW
1291 blkif->st_wr_sect += preq.nr_sects;
1292
fc53bf75 1293 return 0;
4d05a28d
KRW
1294
1295 fail_flush:
bb642e83 1296 xen_blkbk_unmap(blkif, pending_req->segments,
31552ee3 1297 pending_req->nr_pages);
4d05a28d 1298 fail_response:
0faa8cca 1299 /* Haven't submitted any bio's yet. */
402b27f9 1300 make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
bf0720c4 1301 free_req(blkif, pending_req);
4d05a28d 1302 msleep(1); /* back off a bit */
fc53bf75 1303 return -EIO;
4d05a28d
KRW
1304
1305 fail_put_bio:
03e0edf9 1306 for (i = 0; i < nbio; i++)
77089926 1307 bio_put(biolist[i]);
0e5e098a 1308 atomic_set(&pending_req->pendcnt, 1);
4d05a28d 1309 __end_block_io_op(pending_req, -EINVAL);
4d05a28d 1310 msleep(1); /* back off a bit */
fc53bf75 1311 return -EIO;
4d05a28d
KRW
1312}
1313
1314
1315
a1397fa3
KRW
1316/*
1317 * Put a response on the ring on how the operation fared.
4d05a28d 1318 */
30fd1502 1319static void make_response(struct xen_blkif *blkif, u64 id,
4d05a28d
KRW
1320 unsigned short op, int st)
1321{
88122933 1322 struct blkif_response resp;
4d05a28d 1323 unsigned long flags;
88122933 1324 union blkif_back_rings *blk_rings = &blkif->blk_rings;
4d05a28d
KRW
1325 int notify;
1326
1327 resp.id = id;
1328 resp.operation = op;
1329 resp.status = st;
1330
1331 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1332 /* Place on the response ring for the relevant domain. */
1333 switch (blkif->blk_protocol) {
1334 case BLKIF_PROTOCOL_NATIVE:
1335 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1336 &resp, sizeof(resp));
1337 break;
1338 case BLKIF_PROTOCOL_X86_32:
1339 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1340 &resp, sizeof(resp));
1341 break;
1342 case BLKIF_PROTOCOL_X86_64:
1343 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1344 &resp, sizeof(resp));
1345 break;
1346 default:
1347 BUG();
1348 }
1349 blk_rings->common.rsp_prod_pvt++;
1350 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
4d05a28d 1351 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
4d05a28d
KRW
1352 if (notify)
1353 notify_remote_via_irq(blkif->irq);
1354}
1355
8b6bf747 1356static int __init xen_blkif_init(void)
4d05a28d 1357{
8770b268 1358 int rc = 0;
4d05a28d 1359
b2167ba6 1360 if (!xen_domain())
4d05a28d
KRW
1361 return -ENODEV;
1362
8b6bf747 1363 rc = xen_blkif_interface_init();
8770b268
KRW
1364 if (rc)
1365 goto failed_init;
4d05a28d 1366
8b6bf747 1367 rc = xen_blkif_xenbus_init();
8770b268
KRW
1368 if (rc)
1369 goto failed_init;
4d05a28d 1370
8770b268 1371 failed_init:
8770b268 1372 return rc;
4d05a28d
KRW
1373}
1374
8b6bf747 1375module_init(xen_blkif_init);
4d05a28d
KRW
1376
1377MODULE_LICENSE("Dual BSD/GPL");
a7e9357f 1378MODULE_ALIAS("xen-backend:vbd");