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