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