blob: 64c60edcdfbc5546cd84538986b5d26625f9f248 [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/******************************************************************************
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04002 *
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:
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04007 * drivers/block/xen-blkfront.c
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04008 *
9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10 * Copyright (c) 2005, Christopher Clark
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 * IN THE SOFTWARE.
35 */
36
37#include <linux/spinlock.h>
38#include <linux/kthread.h>
39#include <linux/list.h>
40#include <linux/delay.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080041#include <linux/freezer.h>
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020042#include <linux/bitmap.h>
Jeremy Fitzhardingeafd91d02009-09-15 14:12:37 -070043
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080044#include <xen/events.h>
45#include <xen/page.h>
Stefano Stabellinie79affc2012-08-08 17:21:14 +000046#include <xen/xen.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080047#include <asm/xen/hypervisor.h>
48#include <asm/xen/hypercall.h>
Roger Pau Monne087ffec2013-02-14 11:12:09 +010049#include <xen/balloon.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040050#include "common.h"
51
52/*
Roger Pau Monnec6cc1422013-04-17 20:18:56 +020053 * Maximum number of unused free pages to keep in the internal buffer.
54 * Setting this to a value too low will reduce memory used in each backend,
55 * but can have a performance penalty.
56 *
57 * A sane value is xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST, but can
58 * be set to a lower value that might degrade performance on some intensive
59 * IO workloads.
60 */
61
Roger Pau Monne402b27f2013-04-18 16:06:54 +020062static int xen_blkif_max_buffer_pages = 1024;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +020063module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
64MODULE_PARM_DESC(max_buffer_pages,
65"Maximum number of free pages to keep in each block backend buffer");
66
Roger Pau Monne3f3aad52013-04-17 20:18:57 +020067/*
68 * Maximum number of grants to map persistently in blkback. For maximum
69 * performance this should be the total numbers of grants that can be used
70 * to fill the ring, but since this might become too high, specially with
71 * the use of indirect descriptors, we set it to a value that provides good
72 * performance without using too much memory.
73 *
74 * When the list of persistent grants is full we clean it up using a LRU
75 * algorithm.
76 */
77
Roger Pau Monne402b27f2013-04-18 16:06:54 +020078static int xen_blkif_max_pgrants = 1056;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +020079module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
80MODULE_PARM_DESC(max_persistent_grants,
81 "Maximum number of grants to map persistently");
82
83/*
84 * The LRU mechanism to clean the lists of persistent grants needs to
85 * be executed periodically. The time interval between consecutive executions
86 * of the purge mechanism is set in ms.
87 */
88#define LRU_INTERVAL 100
89
90/*
91 * When the persistent grants list is full we will remove unused grants
92 * from the list. The percent number of grants to be removed at each LRU
93 * execution.
94 */
95#define LRU_PERCENT_CLEAN 5
96
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040097/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040098static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040099module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400100
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400101#define BLKBACK_INVALID_HANDLE (~0)
102
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200103/* Number of free pages to remove on each call to free_xenballooned_pages */
104#define NUM_BATCH_FREE_PAGES 10
105
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200106static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400107{
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200108 unsigned long flags;
109
110 spin_lock_irqsave(&blkif->free_pages_lock, flags);
111 if (list_empty(&blkif->free_pages)) {
112 BUG_ON(blkif->free_pages_num != 0);
113 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
114 return alloc_xenballooned_pages(1, page, false);
115 }
116 BUG_ON(blkif->free_pages_num == 0);
117 page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
118 list_del(&page[0]->lru);
119 blkif->free_pages_num--;
120 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
121
122 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400123}
124
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200125static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
126 int num)
127{
128 unsigned long flags;
129 int i;
130
131 spin_lock_irqsave(&blkif->free_pages_lock, flags);
132 for (i = 0; i < num; i++)
133 list_add(&page[i]->lru, &blkif->free_pages);
134 blkif->free_pages_num += num;
135 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
136}
137
138static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
139{
140 /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
141 struct page *page[NUM_BATCH_FREE_PAGES];
142 unsigned int num_pages = 0;
143 unsigned long flags;
144
145 spin_lock_irqsave(&blkif->free_pages_lock, flags);
146 while (blkif->free_pages_num > num) {
147 BUG_ON(list_empty(&blkif->free_pages));
148 page[num_pages] = list_first_entry(&blkif->free_pages,
149 struct page, lru);
150 list_del(&page[num_pages]->lru);
151 blkif->free_pages_num--;
152 if (++num_pages == NUM_BATCH_FREE_PAGES) {
153 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
154 free_xenballooned_pages(num_pages, page);
155 spin_lock_irqsave(&blkif->free_pages_lock, flags);
156 num_pages = 0;
157 }
158 }
159 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
160 if (num_pages != 0)
161 free_xenballooned_pages(num_pages, page);
162}
163
164#define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
165
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400166static int do_block_io_op(struct xen_blkif *blkif);
167static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400168 struct blkif_request *req,
169 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400170static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400171 unsigned short op, int st);
172
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100173#define foreach_grant_safe(pos, n, rbtree, node) \
174 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
Roger Pau Monne217fd5e2013-03-18 17:49:33 +0100175 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200176 &(pos)->node != NULL; \
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100177 (pos) = container_of(n, typeof(*(pos)), node), \
178 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200179
180
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200181/*
182 * We don't need locking around the persistent grant helpers
183 * because blkback uses a single-thread for each backed, so we
184 * can be sure that this functions will never be called recursively.
185 *
186 * The only exception to that is put_persistent_grant, that can be called
187 * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
188 * bit operations to modify the flags of a persistent grant and to count
189 * the number of used grants.
190 */
191static int add_persistent_gnt(struct xen_blkif *blkif,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200192 struct persistent_gnt *persistent_gnt)
193{
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200194 struct rb_node **new = NULL, *parent = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200195 struct persistent_gnt *this;
196
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200197 if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
198 if (!blkif->vbd.overflow_max_grants)
199 blkif->vbd.overflow_max_grants = 1;
200 return -EBUSY;
201 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200202 /* Figure out where to put new node */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200203 new = &blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200204 while (*new) {
205 this = container_of(*new, struct persistent_gnt, node);
206
207 parent = *new;
208 if (persistent_gnt->gnt < this->gnt)
209 new = &((*new)->rb_left);
210 else if (persistent_gnt->gnt > this->gnt)
211 new = &((*new)->rb_right);
212 else {
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200213 pr_alert_ratelimited(DRV_PFX " trying to add a gref that's already in the tree\n");
214 return -EINVAL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200215 }
216 }
217
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200218 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
219 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200220 /* Add new node and rebalance tree. */
221 rb_link_node(&(persistent_gnt->node), parent, new);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200222 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
223 blkif->persistent_gnt_c++;
224 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200225 return 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200226}
227
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200228static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200229 grant_ref_t gref)
230{
231 struct persistent_gnt *data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200232 struct rb_node *node = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200233
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200234 node = blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200235 while (node) {
236 data = container_of(node, struct persistent_gnt, node);
237
238 if (gref < data->gnt)
239 node = node->rb_left;
240 else if (gref > data->gnt)
241 node = node->rb_right;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200242 else {
243 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
244 pr_alert_ratelimited(DRV_PFX " requesting a grant already in use\n");
245 return NULL;
246 }
247 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
248 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200249 return data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200250 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200251 }
252 return NULL;
253}
254
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200255static void put_persistent_gnt(struct xen_blkif *blkif,
256 struct persistent_gnt *persistent_gnt)
257{
258 if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
259 pr_alert_ratelimited(DRV_PFX " freeing a grant already unused");
260 set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
261 clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
262 atomic_dec(&blkif->persistent_gnt_in_use);
263}
264
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200265static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
266 unsigned int num)
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100267{
268 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
269 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
270 struct persistent_gnt *persistent_gnt;
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100271 struct rb_node *n;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100272 int ret = 0;
273 int segs_to_unmap = 0;
274
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100275 foreach_grant_safe(persistent_gnt, n, root, node) {
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100276 BUG_ON(persistent_gnt->handle ==
277 BLKBACK_INVALID_HANDLE);
278 gnttab_set_unmap_op(&unmap[segs_to_unmap],
279 (unsigned long) pfn_to_kaddr(page_to_pfn(
280 persistent_gnt->page)),
281 GNTMAP_host_map,
282 persistent_gnt->handle);
283
284 pages[segs_to_unmap] = persistent_gnt->page;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100285
286 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
287 !rb_next(&persistent_gnt->node)) {
288 ret = gnttab_unmap_refs(unmap, NULL, pages,
289 segs_to_unmap);
290 BUG_ON(ret);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200291 put_free_pages(blkif, pages, segs_to_unmap);
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100292 segs_to_unmap = 0;
293 }
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100294
295 rb_erase(&persistent_gnt->node, root);
296 kfree(persistent_gnt);
297 num--;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100298 }
299 BUG_ON(num != 0);
300}
301
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700302void xen_blkbk_unmap_purged_grants(struct work_struct *work)
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200303{
304 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
305 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
306 struct persistent_gnt *persistent_gnt;
307 int ret, segs_to_unmap = 0;
308 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
309
310 while(!list_empty(&blkif->persistent_purge_list)) {
311 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
312 struct persistent_gnt,
313 remove_node);
314 list_del(&persistent_gnt->remove_node);
315
316 gnttab_set_unmap_op(&unmap[segs_to_unmap],
317 vaddr(persistent_gnt->page),
318 GNTMAP_host_map,
319 persistent_gnt->handle);
320
321 pages[segs_to_unmap] = persistent_gnt->page;
322
323 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
324 ret = gnttab_unmap_refs(unmap, NULL, pages,
325 segs_to_unmap);
326 BUG_ON(ret);
327 put_free_pages(blkif, pages, segs_to_unmap);
328 segs_to_unmap = 0;
329 }
330 kfree(persistent_gnt);
331 }
332 if (segs_to_unmap > 0) {
333 ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
334 BUG_ON(ret);
335 put_free_pages(blkif, pages, segs_to_unmap);
336 }
337}
338
339static void purge_persistent_gnt(struct xen_blkif *blkif)
340{
341 struct persistent_gnt *persistent_gnt;
342 struct rb_node *n;
343 unsigned int num_clean, total;
Roger Pau Monne2d910542013-06-21 12:56:53 +0200344 bool scan_used = false, clean_used = false;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200345 struct rb_root *root;
346
347 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
348 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
349 !blkif->vbd.overflow_max_grants)) {
350 return;
351 }
352
353 if (work_pending(&blkif->persistent_purge_work)) {
354 pr_alert_ratelimited(DRV_PFX "Scheduled work from previous purge is still pending, cannot purge list\n");
355 return;
356 }
357
358 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
359 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
360 num_clean = min(blkif->persistent_gnt_c, num_clean);
Roger Pau Monne2d910542013-06-21 12:56:53 +0200361 if ((num_clean == 0) ||
362 (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200363 return;
364
365 /*
366 * At this point, we can assure that there will be no calls
367 * to get_persistent_grant (because we are executing this code from
368 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
369 * which means that the number of currently used grants will go down,
370 * but never up, so we will always be able to remove the requested
371 * number of grants.
372 */
373
374 total = num_clean;
375
376 pr_debug(DRV_PFX "Going to purge %u persistent grants\n", num_clean);
377
Roger Pau Monneef753412014-02-04 11:26:13 +0100378 BUG_ON(!list_empty(&blkif->persistent_purge_list));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200379 root = &blkif->persistent_gnts;
380purge_list:
381 foreach_grant_safe(persistent_gnt, n, root, node) {
382 BUG_ON(persistent_gnt->handle ==
383 BLKBACK_INVALID_HANDLE);
384
Roger Pau Monne2d910542013-06-21 12:56:53 +0200385 if (clean_used) {
386 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
387 continue;
388 }
389
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200390 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
391 continue;
392 if (!scan_used &&
393 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
394 continue;
395
396 rb_erase(&persistent_gnt->node, root);
397 list_add(&persistent_gnt->remove_node,
398 &blkif->persistent_purge_list);
399 if (--num_clean == 0)
400 goto finished;
401 }
402 /*
403 * If we get here it means we also need to start cleaning
404 * grants that were used since last purge in order to cope
405 * with the requested num
406 */
Roger Pau Monne2d910542013-06-21 12:56:53 +0200407 if (!scan_used && !clean_used) {
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200408 pr_debug(DRV_PFX "Still missing %u purged frames\n", num_clean);
409 scan_used = true;
410 goto purge_list;
411 }
412finished:
Roger Pau Monne2d910542013-06-21 12:56:53 +0200413 if (!clean_used) {
414 pr_debug(DRV_PFX "Finished scanning for grants to clean, removing used flag\n");
415 clean_used = true;
416 goto purge_list;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200417 }
Roger Pau Monne2d910542013-06-21 12:56:53 +0200418
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200419 blkif->persistent_gnt_c -= (total - num_clean);
420 blkif->vbd.overflow_max_grants = 0;
421
422 /* We can defer this work */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200423 schedule_work(&blkif->persistent_purge_work);
424 pr_debug(DRV_PFX "Purged %u/%u\n", (total - num_clean), total);
425 return;
426}
427
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400428/*
429 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400430 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200431static struct pending_req *alloc_req(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400432{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400433 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400434 unsigned long flags;
435
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200436 spin_lock_irqsave(&blkif->pending_free_lock, flags);
437 if (!list_empty(&blkif->pending_free)) {
438 req = list_entry(blkif->pending_free.next, struct pending_req,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400439 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400440 list_del(&req->free_list);
441 }
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200442 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400443 return req;
444}
445
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400446/*
447 * Return the 'pending_req' structure back to the freepool. We also
448 * wake up the thread if it was waiting for a free page.
449 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200450static void free_req(struct xen_blkif *blkif, struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400451{
452 unsigned long flags;
453 int was_empty;
454
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200455 spin_lock_irqsave(&blkif->pending_free_lock, flags);
456 was_empty = list_empty(&blkif->pending_free);
457 list_add(&req->free_list, &blkif->pending_free);
458 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400459 if (was_empty)
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200460 wake_up(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400461}
462
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400463/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400464 * Routines for managing virtual block devices (vbds).
465 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400466static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
467 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400468{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400469 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400470 int rc = -EACCES;
471
472 if ((operation != READ) && vbd->readonly)
473 goto out;
474
Jan Beulich8ab52152011-05-17 11:07:05 +0100475 if (likely(req->nr_sects)) {
476 blkif_sector_t end = req->sector_number + req->nr_sects;
477
478 if (unlikely(end < req->sector_number))
479 goto out;
480 if (unlikely(end > vbd_sz(vbd)))
481 goto out;
482 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400483
484 req->dev = vbd->pdevice;
485 req->bdev = vbd->bdev;
486 rc = 0;
487
488 out:
489 return rc;
490}
491
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400492static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400493{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400494 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400495 struct xenbus_transaction xbt;
496 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400497 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400498 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400499
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400500 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400501 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400502 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400503 vbd->size = new_size;
504again:
505 err = xenbus_transaction_start(&xbt);
506 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400507 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400508 return;
509 }
510 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400511 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400512 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400513 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400514 goto abort;
515 }
516 /*
517 * Write the current state; we will use this to synchronize
518 * the front-end. If the current state is "connected" the
519 * front-end will get the new size information online.
520 */
521 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
522 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400523 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400524 goto abort;
525 }
526
527 err = xenbus_transaction_end(xbt, 0);
528 if (err == -EAGAIN)
529 goto again;
530 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400531 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400532 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400533abort:
534 xenbus_transaction_end(xbt, 1);
535}
536
537/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400538 * Notification from the guest OS.
539 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400540static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400541{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400542 blkif->waiting_reqs = 1;
543 wake_up(&blkif->wq);
544}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400545
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400546irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400547{
548 blkif_notify_work(dev_id);
549 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400550}
551
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400552/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400553 * SCHEDULER FUNCTIONS
554 */
555
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400556static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400557{
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000558 pr_info("xen-blkback (%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200559 " | ds %4llu | pg: %4u/%4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400560 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800561 blkif->st_rd_req, blkif->st_wr_req,
Roger Pau Monnec1a15d02013-04-17 20:18:55 +0200562 blkif->st_f_req, blkif->st_ds_req,
563 blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200564 xen_blkif_max_pgrants);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400565 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
566 blkif->st_rd_req = 0;
567 blkif->st_wr_req = 0;
568 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800569 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400570}
571
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400572int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400573{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400574 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400575 struct xen_vbd *vbd = &blkif->vbd;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200576 unsigned long timeout;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500577 int ret;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400578
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400579 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400580
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400581 while (!kthread_should_stop()) {
582 if (try_to_freeze())
583 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400584 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400585 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400586
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200587 timeout = msecs_to_jiffies(LRU_INTERVAL);
588
589 timeout = wait_event_interruptible_timeout(
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400590 blkif->wq,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200591 blkif->waiting_reqs || kthread_should_stop(),
592 timeout);
593 if (timeout == 0)
594 goto purge_gnt_list;
595 timeout = wait_event_interruptible_timeout(
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200596 blkif->pending_free_wq,
597 !list_empty(&blkif->pending_free) ||
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200598 kthread_should_stop(),
599 timeout);
600 if (timeout == 0)
601 goto purge_gnt_list;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400602
603 blkif->waiting_reqs = 0;
604 smp_mb(); /* clear flag *before* checking for work */
605
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500606 ret = do_block_io_op(blkif);
607 if (ret > 0)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400608 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500609 if (ret == -EACCES)
610 wait_event_interruptible(blkif->shutdown_wq,
611 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400612
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200613purge_gnt_list:
614 if (blkif->vbd.feature_gnt_persistent &&
615 time_after(jiffies, blkif->next_lru)) {
616 purge_persistent_gnt(blkif);
617 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
618 }
619
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200620 /* Shrink if we have more than xen_blkif_max_buffer_pages */
621 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
622
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400623 if (log_stats && time_after(jiffies, blkif->st_print))
624 print_stats(blkif);
625 }
626
Roger Pau Monneef753412014-02-04 11:26:13 +0100627 /* Drain pending purge work */
628 flush_work(&blkif->persistent_purge_work);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200629
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400630 if (log_stats)
631 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400632
633 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400634 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400635
636 return 0;
637}
638
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400639/*
Roger Pau Monneef753412014-02-04 11:26:13 +0100640 * Remove persistent grants and empty the pool of free pages
641 */
642void xen_blkbk_free_caches(struct xen_blkif *blkif)
643{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400644 /* Free all persistent grant pages */
645 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
646 free_persistent_gnts(blkif, &blkif->persistent_gnts,
647 blkif->persistent_gnt_c);
648
649 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
650 blkif->persistent_gnt_c = 0;
651
Matt Rushton2ed22e32014-02-04 11:26:12 +0100652 /* Since we are shutting down remove all pages from the buffer */
653 shrink_free_pagepool(blkif, 0 /* All */);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400654}
655
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400656/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400657 * Unmap the grant references, and also remove the M2P over-rides
658 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400659 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200660static void xen_blkbk_unmap(struct xen_blkif *blkif,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200661 struct grant_page *pages[],
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200662 int num)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400663{
664 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200665 struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400666 unsigned int i, invcount = 0;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400667 int ret;
668
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200669 for (i = 0; i < num; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200670 if (pages[i]->persistent_gnt != NULL) {
671 put_persistent_gnt(blkif, pages[i]->persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200672 continue;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200673 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200674 if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400675 continue;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200676 unmap_pages[invcount] = pages[i]->page;
677 gnttab_set_unmap_op(&unmap[invcount], vaddr(pages[i]->page),
678 GNTMAP_host_map, pages[i]->handle);
679 pages[i]->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200680 if (++invcount == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
681 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages,
682 invcount);
683 BUG_ON(ret);
684 put_free_pages(blkif, unmap_pages, invcount);
685 invcount = 0;
686 }
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400687 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200688 if (invcount) {
689 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
690 BUG_ON(ret);
691 put_free_pages(blkif, unmap_pages, invcount);
692 }
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400693}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400694
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200695static int xen_blkbk_map(struct xen_blkif *blkif,
696 struct grant_page *pages[],
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200697 int num, bool ro)
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400698{
699 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200700 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
701 struct persistent_gnt *persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200702 phys_addr_t addr = 0;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200703 int i, seg_idx, new_map_idx;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200704 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400705 int ret = 0;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200706 int last_map = 0, map_until = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200707 int use_persistent_gnts;
708
709 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
710
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400711 /*
712 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400713 * assign map[..] with the PFN of the page in our domain with the
714 * corresponding grant reference for each page.
715 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200716again:
717 for (i = map_until; i < num; i++) {
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400718 uint32_t flags;
719
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200720 if (use_persistent_gnts)
721 persistent_gnt = get_persistent_gnt(
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200722 blkif,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200723 pages[i]->gref);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200724
725 if (persistent_gnt) {
726 /*
727 * We are using persistent grants and
728 * the grant is already mapped
729 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200730 pages[i]->page = persistent_gnt->page;
731 pages[i]->persistent_gnt = persistent_gnt;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200732 } else {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200733 if (get_free_page(blkif, &pages[i]->page))
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200734 goto out_of_memory;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200735 addr = vaddr(pages[i]->page);
736 pages_to_gnt[segs_to_map] = pages[i]->page;
737 pages[i]->persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200738 flags = GNTMAP_host_map;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200739 if (!use_persistent_gnts && ro)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200740 flags |= GNTMAP_readonly;
741 gnttab_set_map_op(&map[segs_to_map++], addr,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200742 flags, pages[i]->gref,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200743 blkif->domid);
744 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200745 map_until = i + 1;
746 if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
747 break;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400748 }
749
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200750 if (segs_to_map) {
751 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
752 BUG_ON(ret);
753 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400754
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400755 /*
756 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400757 * so that when we access vaddr(pending_req,i) it has the contents of
758 * the page from the other domain.
759 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200760 for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200761 if (!pages[seg_idx]->persistent_gnt) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200762 /* This is a newly mapped grant */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200763 BUG_ON(new_map_idx >= segs_to_map);
764 if (unlikely(map[new_map_idx].status != 0)) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200765 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200766 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200767 ret |= 1;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200768 goto next;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200769 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200770 pages[seg_idx]->handle = map[new_map_idx].handle;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200771 } else {
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200772 continue;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200773 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200774 if (use_persistent_gnts &&
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200775 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200776 /*
777 * We are using persistent grants, the grant is
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200778 * not mapped but we might have room for it.
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200779 */
780 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
781 GFP_KERNEL);
782 if (!persistent_gnt) {
783 /*
784 * If we don't have enough memory to
785 * allocate the persistent_gnt struct
786 * map this grant non-persistenly
787 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200788 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200789 }
790 persistent_gnt->gnt = map[new_map_idx].ref;
791 persistent_gnt->handle = map[new_map_idx].handle;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200792 persistent_gnt->page = pages[seg_idx]->page;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200793 if (add_persistent_gnt(blkif,
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200794 persistent_gnt)) {
795 kfree(persistent_gnt);
796 persistent_gnt = NULL;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200797 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200798 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200799 pages[seg_idx]->persistent_gnt = persistent_gnt;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200800 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
801 persistent_gnt->gnt, blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200802 xen_blkif_max_pgrants);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200803 goto next;
804 }
805 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
806 blkif->vbd.overflow_max_grants = 1;
807 pr_debug(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
808 blkif->domid, blkif->vbd.handle);
809 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200810 /*
811 * We could not map this grant persistently, so use it as
812 * a non-persistent grant.
813 */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200814next:
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200815 new_map_idx++;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400816 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200817 segs_to_map = 0;
818 last_map = map_until;
819 if (map_until != num)
820 goto again;
821
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400822 return ret;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200823
824out_of_memory:
825 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
826 put_free_pages(blkif, pages_to_gnt, segs_to_map);
827 return -ENOMEM;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400828}
829
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200830static int xen_blkbk_map_seg(struct pending_req *pending_req)
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200831{
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200832 int rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200833
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200834 rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200835 pending_req->nr_pages,
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200836 (pending_req->operation != BLKIF_OP_READ));
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200837
838 return rc;
839}
840
841static int xen_blkbk_parse_indirect(struct blkif_request *req,
842 struct pending_req *pending_req,
843 struct seg_buf seg[],
844 struct phys_req *preq)
845{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200846 struct grant_page **pages = pending_req->indirect_pages;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200847 struct xen_blkif *blkif = pending_req->blkif;
848 int indirect_grefs, rc, n, nseg, i;
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100849 struct blkif_request_segment *segments = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200850
851 nseg = pending_req->nr_pages;
852 indirect_grefs = INDIRECT_PAGES(nseg);
853 BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
854
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200855 for (i = 0; i < indirect_grefs; i++)
856 pages[i]->gref = req->u.indirect.indirect_grefs[i];
857
858 rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200859 if (rc)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200860 goto unmap;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200861
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200862 for (n = 0, i = 0; n < nseg; n++) {
863 if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
864 /* Map indirect segments */
865 if (segments)
866 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200867 segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200868 }
869 i = n % SEGS_PER_INDIRECT_FRAME;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200870 pending_req->segments[n]->gref = segments[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200871 seg[n].nsec = segments[i].last_sect -
872 segments[i].first_sect + 1;
873 seg[n].offset = (segments[i].first_sect << 9);
874 if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
875 (segments[i].last_sect < segments[i].first_sect)) {
876 rc = -EINVAL;
877 goto unmap;
878 }
879 preq->nr_sects += seg[n].nsec;
880 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200881
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200882unmap:
883 if (segments)
884 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200885 xen_blkbk_unmap(blkif, pages, indirect_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200886 return rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200887}
888
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400889static int dispatch_discard_io(struct xen_blkif *blkif,
890 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800891{
892 int err = 0;
893 int status = BLKIF_RSP_OKAY;
894 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400895 unsigned long secure;
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500896 struct phys_req preq;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800897
Vegard Nossumea5ec762013-09-05 13:00:14 +0200898 xen_blkif_get(blkif);
899
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500900 preq.sector_number = req->u.discard.sector_number;
901 preq.nr_sects = req->u.discard.nr_sectors;
902
903 err = xen_vbd_translate(&preq, blkif, WRITE);
904 if (err) {
905 pr_warn(DRV_PFX "access denied: DISCARD [%llu->%llu] on dev=%04x\n",
906 preq.sector_number,
907 preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
908 goto fail_response;
909 }
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400910 blkif->st_ds_req++;
911
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400912 secure = (blkif->vbd.discard_secure &&
913 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
914 BLKDEV_DISCARD_SECURE : 0;
915
916 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
917 req->u.discard.nr_sectors,
918 GFP_KERNEL, secure);
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500919fail_response:
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800920 if (err == -EOPNOTSUPP) {
921 pr_debug(DRV_PFX "discard op failed, not supported\n");
922 status = BLKIF_RSP_EOPNOTSUPP;
923 } else if (err)
924 status = BLKIF_RSP_ERROR;
925
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400926 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400927 xen_blkif_put(blkif);
928 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800929}
930
David Vrabel0e367ae2013-03-07 17:32:01 +0000931static int dispatch_other_io(struct xen_blkif *blkif,
932 struct blkif_request *req,
933 struct pending_req *pending_req)
934{
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200935 free_req(blkif, pending_req);
David Vrabel0e367ae2013-03-07 17:32:01 +0000936 make_response(blkif, req->u.other.id, req->operation,
937 BLKIF_RSP_EOPNOTSUPP);
938 return -EIO;
939}
940
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400941static void xen_blk_drain_io(struct xen_blkif *blkif)
942{
943 atomic_set(&blkif->drain, 1);
944 do {
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100945 if (atomic_read(&blkif->inflight) == 0)
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -0400946 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400947 wait_for_completion_interruptible_timeout(
948 &blkif->drain_complete, HZ);
949
950 if (!atomic_read(&blkif->drain))
951 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400952 } while (!kthread_should_stop());
953 atomic_set(&blkif->drain, 0);
954}
955
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400956/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400957 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400958 */
959
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400960static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400961{
962 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400963 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400964 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400965 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400966 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400967 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400968 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
969 (error == -EOPNOTSUPP)) {
970 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
971 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
972 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400973 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400974 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400975 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400976 pending_req->status = BLKIF_RSP_ERROR;
977 }
978
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400979 /*
980 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400981 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400982 * the proper response on the ring.
983 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400984 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100985 struct xen_blkif *blkif = pending_req->blkif;
986
987 xen_blkbk_unmap(blkif,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200988 pending_req->segments,
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200989 pending_req->nr_pages);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100990 make_response(blkif, pending_req->id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400991 pending_req->operation, pending_req->status);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100992 free_req(blkif, pending_req);
993 /*
994 * Make sure the request is freed before releasing blkif,
995 * or there could be a race between free_req and the
996 * cleanup done in xen_blkif_free during shutdown.
997 *
998 * NB: The fact that we might try to wake up pending_free_wq
999 * before drain_complete (in case there's a drain going on)
1000 * it's not a problem with our current implementation
1001 * because we can assure there's no thread waiting on
1002 * pending_free_wq if there's a drain going on, but it has
1003 * to be taken into account if the current model is changed.
1004 */
1005 if (atomic_dec_and_test(&blkif->inflight) && atomic_read(&blkif->drain)) {
1006 complete(&blkif->drain_complete);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001007 }
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001008 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001009 }
1010}
1011
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001012/*
1013 * bio callback.
1014 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001015static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001016{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001017 __end_block_io_op(bio->bi_private, error);
1018 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001019}
1020
1021
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001022
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001023/*
1024 * Function to copy the from the ring buffer the 'struct blkif_request'
1025 * (which has the sectors we want, number of them, grant references, etc),
1026 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001027 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001028static int
1029__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001030{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001031 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1032 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001033 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001034 RING_IDX rc, rp;
1035 int more_to_do = 0;
1036
1037 rc = blk_rings->common.req_cons;
1038 rp = blk_rings->common.sring->req_prod;
1039 rmb(); /* Ensure we see queued requests up to 'rp'. */
1040
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -05001041 if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
1042 rc = blk_rings->common.rsp_prod_pvt;
1043 pr_warn(DRV_PFX "Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
1044 rp, rc, rp - rc, blkif->vbd.pdevice);
1045 return -EACCES;
1046 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001047 while (rc != rp) {
1048
1049 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1050 break;
1051
Keir Fraser8270b452009-03-06 08:29:15 +00001052 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001053 more_to_do = 1;
1054 break;
1055 }
1056
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001057 pending_req = alloc_req(blkif);
Keir Fraser8270b452009-03-06 08:29:15 +00001058 if (NULL == pending_req) {
1059 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001060 more_to_do = 1;
1061 break;
1062 }
1063
1064 switch (blkif->blk_protocol) {
1065 case BLKIF_PROTOCOL_NATIVE:
1066 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1067 break;
1068 case BLKIF_PROTOCOL_X86_32:
1069 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1070 break;
1071 case BLKIF_PROTOCOL_X86_64:
1072 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1073 break;
1074 default:
1075 BUG();
1076 }
1077 blk_rings->common.req_cons = ++rc; /* before make_response() */
1078
1079 /* Apply all sanity checks to /private copy/ of request. */
1080 barrier();
David Vrabel0e367ae2013-03-07 17:32:01 +00001081
1082 switch (req.operation) {
1083 case BLKIF_OP_READ:
1084 case BLKIF_OP_WRITE:
1085 case BLKIF_OP_WRITE_BARRIER:
1086 case BLKIF_OP_FLUSH_DISKCACHE:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001087 case BLKIF_OP_INDIRECT:
David Vrabel0e367ae2013-03-07 17:32:01 +00001088 if (dispatch_rw_block_io(blkif, &req, pending_req))
1089 goto done;
1090 break;
1091 case BLKIF_OP_DISCARD:
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001092 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001093 if (dispatch_discard_io(blkif, &req))
David Vrabel0e367ae2013-03-07 17:32:01 +00001094 goto done;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001095 break;
David Vrabel0e367ae2013-03-07 17:32:01 +00001096 default:
1097 if (dispatch_other_io(blkif, &req, pending_req))
1098 goto done;
1099 break;
1100 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001101
1102 /* Yield point for this unbounded loop. */
1103 cond_resched();
1104 }
David Vrabel0e367ae2013-03-07 17:32:01 +00001105done:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001106 return more_to_do;
1107}
1108
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001109static int
1110do_block_io_op(struct xen_blkif *blkif)
1111{
1112 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1113 int more_to_do;
1114
1115 do {
1116 more_to_do = __do_block_io_op(blkif);
1117 if (more_to_do)
1118 break;
1119
1120 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1121 } while (more_to_do);
1122
1123 return more_to_do;
1124}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001125/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001126 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1127 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001128 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001129static int dispatch_rw_block_io(struct xen_blkif *blkif,
1130 struct blkif_request *req,
1131 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001132{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001133 struct phys_req preq;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001134 struct seg_buf *seg = pending_req->seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001135 unsigned int nseg;
1136 struct bio *bio = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001137 struct bio **biolist = pending_req->biolist;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001138 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001139 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001140 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001141 bool drain = false;
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001142 struct grant_page **pages = pending_req->segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001143 unsigned short req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001144
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001145 req_operation = req->operation == BLKIF_OP_INDIRECT ?
1146 req->u.indirect.indirect_op : req->operation;
1147 if ((req->operation == BLKIF_OP_INDIRECT) &&
1148 (req_operation != BLKIF_OP_READ) &&
1149 (req_operation != BLKIF_OP_WRITE)) {
1150 pr_debug(DRV_PFX "Invalid indirect operation (%u)\n",
1151 req_operation);
1152 goto fail_response;
1153 }
1154
1155 switch (req_operation) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001156 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001157 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001158 operation = READ;
1159 break;
1160 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001161 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -04001162 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001163 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001164 case BLKIF_OP_WRITE_BARRIER:
1165 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001166 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001167 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001168 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001169 break;
1170 default:
1171 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001172 goto fail_response;
1173 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001174 }
1175
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001176 /* Check that the number of segments is sane. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001177 nseg = req->operation == BLKIF_OP_INDIRECT ?
1178 req->u.indirect.nr_segments : req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001179
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001180 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001181 unlikely((req->operation != BLKIF_OP_INDIRECT) &&
1182 (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
1183 unlikely((req->operation == BLKIF_OP_INDIRECT) &&
1184 (nseg > MAX_INDIRECT_SEGMENTS))) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001185 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001186 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001187 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001188 goto fail_response;
1189 }
1190
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001191 preq.nr_sects = 0;
1192
1193 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001194 pending_req->id = req->u.rw.id;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001195 pending_req->operation = req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001196 pending_req->status = BLKIF_RSP_OKAY;
1197 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001198
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001199 if (req->operation != BLKIF_OP_INDIRECT) {
1200 preq.dev = req->u.rw.handle;
1201 preq.sector_number = req->u.rw.sector_number;
1202 for (i = 0; i < nseg; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001203 pages[i]->gref = req->u.rw.seg[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001204 seg[i].nsec = req->u.rw.seg[i].last_sect -
1205 req->u.rw.seg[i].first_sect + 1;
1206 seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
1207 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1208 (req->u.rw.seg[i].last_sect <
1209 req->u.rw.seg[i].first_sect))
1210 goto fail_response;
1211 preq.nr_sects += seg[i].nsec;
1212 }
1213 } else {
1214 preq.dev = req->u.indirect.handle;
1215 preq.sector_number = req->u.indirect.sector_number;
1216 if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001217 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001218 }
1219
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001220 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001221 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001222 operation == READ ? "read" : "write",
1223 preq.sector_number,
Chen Ganga72d9002013-02-28 10:34:23 +08001224 preq.sector_number + preq.nr_sects,
1225 blkif->vbd.pdevice);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001226 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001227 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001228
1229 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001230 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001231 * is set there.
1232 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001233 for (i = 0; i < nseg; i++) {
1234 if (((int)preq.sector_number|(int)seg[i].nsec) &
1235 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001236 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001237 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001238 goto fail_response;
1239 }
1240 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001241
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001242 /* Wait on all outstanding I/O's and once that has been completed
1243 * issue the WRITE_FLUSH.
1244 */
1245 if (drain)
1246 xen_blk_drain_io(pending_req->blkif);
1247
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001248 /*
1249 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001250 * set gnttab_set_unmap_op on all of the grant references and perform
1251 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -04001252 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001253 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001254 if (xen_blkbk_map_seg(pending_req))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001255 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001256
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001257 /*
1258 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1259 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1260 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001261 xen_blkif_get(blkif);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001262 atomic_inc(&blkif->inflight);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001263
1264 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001265 while ((bio == NULL) ||
1266 (bio_add_page(bio,
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001267 pages[i]->page,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001268 seg[i].nsec << 9,
Roger Pau Monneffb1dab2013-03-18 17:49:32 +01001269 seg[i].offset) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001270
Roger Pau Monne1e0f7a22013-06-22 09:59:17 +02001271 int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1272 bio = bio_alloc(GFP_KERNEL, nr_iovecs);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001273 if (unlikely(bio == NULL))
1274 goto fail_put_bio;
1275
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001276 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001277 bio->bi_bdev = preq.bdev;
1278 bio->bi_private = pending_req;
1279 bio->bi_end_io = end_block_io_op;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001280 bio->bi_iter.bi_sector = preq.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001281 }
1282
1283 preq.sector_number += seg[i].nsec;
1284 }
1285
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001286 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001287 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001288 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -04001289
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001290 bio = bio_alloc(GFP_KERNEL, 0);
1291 if (unlikely(bio == NULL))
1292 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001293
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001294 biolist[nbio++] = bio;
1295 bio->bi_bdev = preq.bdev;
1296 bio->bi_private = pending_req;
1297 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001298 }
1299
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001300 atomic_set(&pending_req->pendcnt, nbio);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001301 blk_start_plug(&plug);
1302
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001303 for (i = 0; i < nbio; i++)
1304 submit_bio(operation, biolist[i]);
1305
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001306 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -04001307 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001308
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001309 if (operation == READ)
1310 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -04001311 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001312 blkif->st_wr_sect += preq.nr_sects;
1313
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001314 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001315
1316 fail_flush:
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001317 xen_blkbk_unmap(blkif, pending_req->segments,
Roger Pau Monne31552ee2013-04-17 20:19:00 +02001318 pending_req->nr_pages);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001319 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001320 /* Haven't submitted any bio's yet. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001321 make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001322 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001323 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001324 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001325
1326 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001327 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001328 bio_put(biolist[i]);
Jan Beulich0e5e0982013-03-11 09:39:55 +00001329 atomic_set(&pending_req->pendcnt, 1);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001330 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001331 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001332 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001333}
1334
1335
1336
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001337/*
1338 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001339 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001340static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001341 unsigned short op, int st)
1342{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001343 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001344 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001345 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001346 int notify;
1347
1348 resp.id = id;
1349 resp.operation = op;
1350 resp.status = st;
1351
1352 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1353 /* Place on the response ring for the relevant domain. */
1354 switch (blkif->blk_protocol) {
1355 case BLKIF_PROTOCOL_NATIVE:
1356 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1357 &resp, sizeof(resp));
1358 break;
1359 case BLKIF_PROTOCOL_X86_32:
1360 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1361 &resp, sizeof(resp));
1362 break;
1363 case BLKIF_PROTOCOL_X86_64:
1364 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1365 &resp, sizeof(resp));
1366 break;
1367 default:
1368 BUG();
1369 }
1370 blk_rings->common.rsp_prod_pvt++;
1371 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001372 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001373 if (notify)
1374 notify_remote_via_irq(blkif->irq);
1375}
1376
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001377static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001378{
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001379 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001380
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001381 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001382 return -ENODEV;
1383
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001384 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001385 if (rc)
1386 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001387
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001388 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001389 if (rc)
1390 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001391
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001392 failed_init:
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001393 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001394}
1395
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001396module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001397
1398MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001399MODULE_ALIAS("xen-backend:vbd");