blob: 954c0029fb3babc49d1a1f490f9d420934701e30 [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
Tao Chen77387b82015-04-01 15:04:22 +000037#define pr_fmt(fmt) "xen-blkback: " fmt
38
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040039#include <linux/spinlock.h>
40#include <linux/kthread.h>
41#include <linux/list.h>
42#include <linux/delay.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080043#include <linux/freezer.h>
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020044#include <linux/bitmap.h>
Jeremy Fitzhardingeafd91d02009-09-15 14:12:37 -070045
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080046#include <xen/events.h>
47#include <xen/page.h>
Stefano Stabellinie79affc2012-08-08 17:21:14 +000048#include <xen/xen.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080049#include <asm/xen/hypervisor.h>
50#include <asm/xen/hypercall.h>
Roger Pau Monne087ffec2013-02-14 11:12:09 +010051#include <xen/balloon.h>
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +000052#include <xen/grant_table.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040053#include "common.h"
54
55/*
Roger Pau Monnec6cc1422013-04-17 20:18:56 +020056 * 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
Roger Pau Monne402b27f2013-04-18 16:06:54 +020065static int xen_blkif_max_buffer_pages = 1024;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +020066module_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
Roger Pau Monne3f3aad52013-04-17 20:18:57 +020070/*
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
Roger Pau Monne402b27f2013-04-18 16:06:54 +020081static int xen_blkif_max_pgrants = 1056;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +020082module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
83MODULE_PARM_DESC(max_persistent_grants,
84 "Maximum number of grants to map persistently");
85
86/*
Bob Liu86839c52015-06-03 13:40:03 +080087 * 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");
93/*
Roger Pau Monne3f3aad52013-04-17 20:18:57 +020094 * 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
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400107/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400108static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400109module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400110
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400111#define BLKBACK_INVALID_HANDLE (~0)
112
David Vrabelff4b1562015-01-08 18:06:01 +0000113/* Number of free pages to remove on each call to gnttab_free_pages */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200114#define NUM_BATCH_FREE_PAGES 10
115
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200116static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400117{
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200118 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);
David Vrabelff4b1562015-01-08 18:06:01 +0000124 return gnttab_alloc_pages(1, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200125 }
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);
131
132 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400133}
134
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200135static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
136 int num)
137{
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);
David Vrabelff4b1562015-01-08 18:06:01 +0000164 gnttab_free_pages(num_pages, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200165 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)
David Vrabelff4b1562015-01-08 18:06:01 +0000171 gnttab_free_pages(num_pages, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200172}
173
174#define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
175
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400176static int do_block_io_op(struct xen_blkif *blkif);
177static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400178 struct blkif_request *req,
179 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400180static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400181 unsigned short op, int st);
182
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100183#define foreach_grant_safe(pos, n, rbtree, node) \
184 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
Roger Pau Monne217fd5e2013-03-18 17:49:33 +0100185 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200186 &(pos)->node != NULL; \
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100187 (pos) = container_of(n, typeof(*(pos)), node), \
188 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200189
190
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200191/*
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,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200202 struct persistent_gnt *persistent_gnt)
203{
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200204 struct rb_node **new = NULL, *parent = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200205 struct persistent_gnt *this;
206
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200207 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 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200212 /* Figure out where to put new node */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200213 new = &blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200214 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 {
Tao Chen77387b82015-04-01 15:04:22 +0000223 pr_alert_ratelimited("trying to add a gref that's already in the tree\n");
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200224 return -EINVAL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200225 }
226 }
227
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200228 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
229 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200230 /* Add new node and rebalance tree. */
231 rb_link_node(&(persistent_gnt->node), parent, new);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200232 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
233 blkif->persistent_gnt_c++;
234 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200235 return 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200236}
237
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200238static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200239 grant_ref_t gref)
240{
241 struct persistent_gnt *data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200242 struct rb_node *node = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200243
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200244 node = blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200245 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;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200252 else {
253 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
Tao Chen77387b82015-04-01 15:04:22 +0000254 pr_alert_ratelimited("requesting a grant already in use\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200255 return NULL;
256 }
257 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
258 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200259 return data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200260 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200261 }
262 return NULL;
263}
264
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200265static 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))
Tao Chen77387b82015-04-01 15:04:22 +0000269 pr_alert_ratelimited("freeing a grant already unused\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200270 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
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200275static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
276 unsigned int num)
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100277{
278 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
279 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
280 struct persistent_gnt *persistent_gnt;
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100281 struct rb_node *n;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100282 int segs_to_unmap = 0;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000283 struct gntab_unmap_queue_data unmap_data;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000284
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000285 unmap_data.pages = pages;
286 unmap_data.unmap_ops = unmap;
287 unmap_data.kunmap_ops = NULL;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100288
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100289 foreach_grant_safe(persistent_gnt, n, root, node) {
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100290 BUG_ON(persistent_gnt->handle ==
291 BLKBACK_INVALID_HANDLE);
292 gnttab_set_unmap_op(&unmap[segs_to_unmap],
293 (unsigned long) pfn_to_kaddr(page_to_pfn(
294 persistent_gnt->page)),
295 GNTMAP_host_map,
296 persistent_gnt->handle);
297
298 pages[segs_to_unmap] = persistent_gnt->page;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100299
300 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
301 !rb_next(&persistent_gnt->node)) {
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000302
303 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800304 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000305
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200306 put_free_pages(blkif, pages, segs_to_unmap);
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100307 segs_to_unmap = 0;
308 }
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100309
310 rb_erase(&persistent_gnt->node, root);
311 kfree(persistent_gnt);
312 num--;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100313 }
314 BUG_ON(num != 0);
315}
316
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700317void xen_blkbk_unmap_purged_grants(struct work_struct *work)
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200318{
319 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
320 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
321 struct persistent_gnt *persistent_gnt;
Bob Liu325d73b2015-04-03 14:42:58 +0800322 int segs_to_unmap = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200323 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
Bob Liu325d73b2015-04-03 14:42:58 +0800324 struct gntab_unmap_queue_data unmap_data;
Bob Liu325d73b2015-04-03 14:42:58 +0800325
Bob Liu325d73b2015-04-03 14:42:58 +0800326 unmap_data.pages = pages;
327 unmap_data.unmap_ops = unmap;
328 unmap_data.kunmap_ops = NULL;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200329
330 while(!list_empty(&blkif->persistent_purge_list)) {
331 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
332 struct persistent_gnt,
333 remove_node);
334 list_del(&persistent_gnt->remove_node);
335
336 gnttab_set_unmap_op(&unmap[segs_to_unmap],
337 vaddr(persistent_gnt->page),
338 GNTMAP_host_map,
339 persistent_gnt->handle);
340
341 pages[segs_to_unmap] = persistent_gnt->page;
342
343 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
Bob Liu325d73b2015-04-03 14:42:58 +0800344 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800345 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200346 put_free_pages(blkif, pages, segs_to_unmap);
347 segs_to_unmap = 0;
348 }
349 kfree(persistent_gnt);
350 }
351 if (segs_to_unmap > 0) {
Bob Liu325d73b2015-04-03 14:42:58 +0800352 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800353 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200354 put_free_pages(blkif, pages, segs_to_unmap);
355 }
356}
357
358static void purge_persistent_gnt(struct xen_blkif *blkif)
359{
360 struct persistent_gnt *persistent_gnt;
361 struct rb_node *n;
362 unsigned int num_clean, total;
Roger Pau Monne2d910542013-06-21 12:56:53 +0200363 bool scan_used = false, clean_used = false;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200364 struct rb_root *root;
365
366 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
367 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
368 !blkif->vbd.overflow_max_grants)) {
369 return;
370 }
371
Bob Liu53bc7dc2015-07-22 14:40:10 +0800372 if (work_busy(&blkif->persistent_purge_work)) {
373 pr_alert_ratelimited("Scheduled work from previous purge is still busy, cannot purge list\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200374 return;
375 }
376
377 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
378 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
379 num_clean = min(blkif->persistent_gnt_c, num_clean);
Roger Pau Monne2d910542013-06-21 12:56:53 +0200380 if ((num_clean == 0) ||
381 (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200382 return;
383
384 /*
385 * At this point, we can assure that there will be no calls
386 * to get_persistent_grant (because we are executing this code from
387 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
388 * which means that the number of currently used grants will go down,
389 * but never up, so we will always be able to remove the requested
390 * number of grants.
391 */
392
393 total = num_clean;
394
Tao Chen77387b82015-04-01 15:04:22 +0000395 pr_debug("Going to purge %u persistent grants\n", num_clean);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200396
Roger Pau Monneef753412014-02-04 11:26:13 +0100397 BUG_ON(!list_empty(&blkif->persistent_purge_list));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200398 root = &blkif->persistent_gnts;
399purge_list:
400 foreach_grant_safe(persistent_gnt, n, root, node) {
401 BUG_ON(persistent_gnt->handle ==
402 BLKBACK_INVALID_HANDLE);
403
Roger Pau Monne2d910542013-06-21 12:56:53 +0200404 if (clean_used) {
405 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
406 continue;
407 }
408
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200409 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
410 continue;
411 if (!scan_used &&
412 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
413 continue;
414
415 rb_erase(&persistent_gnt->node, root);
416 list_add(&persistent_gnt->remove_node,
417 &blkif->persistent_purge_list);
418 if (--num_clean == 0)
419 goto finished;
420 }
421 /*
422 * If we get here it means we also need to start cleaning
423 * grants that were used since last purge in order to cope
424 * with the requested num
425 */
Roger Pau Monne2d910542013-06-21 12:56:53 +0200426 if (!scan_used && !clean_used) {
Tao Chen77387b82015-04-01 15:04:22 +0000427 pr_debug("Still missing %u purged frames\n", num_clean);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200428 scan_used = true;
429 goto purge_list;
430 }
431finished:
Roger Pau Monne2d910542013-06-21 12:56:53 +0200432 if (!clean_used) {
Tao Chen77387b82015-04-01 15:04:22 +0000433 pr_debug("Finished scanning for grants to clean, removing used flag\n");
Roger Pau Monne2d910542013-06-21 12:56:53 +0200434 clean_used = true;
435 goto purge_list;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200436 }
Roger Pau Monne2d910542013-06-21 12:56:53 +0200437
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200438 blkif->persistent_gnt_c -= (total - num_clean);
439 blkif->vbd.overflow_max_grants = 0;
440
441 /* We can defer this work */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200442 schedule_work(&blkif->persistent_purge_work);
Tao Chen77387b82015-04-01 15:04:22 +0000443 pr_debug("Purged %u/%u\n", (total - num_clean), total);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200444 return;
445}
446
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400447/*
448 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400449 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200450static struct pending_req *alloc_req(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400451{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400452 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400453 unsigned long flags;
454
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200455 spin_lock_irqsave(&blkif->pending_free_lock, flags);
456 if (!list_empty(&blkif->pending_free)) {
457 req = list_entry(blkif->pending_free.next, struct pending_req,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400458 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400459 list_del(&req->free_list);
460 }
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200461 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400462 return req;
463}
464
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400465/*
466 * Return the 'pending_req' structure back to the freepool. We also
467 * wake up the thread if it was waiting for a free page.
468 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200469static void free_req(struct xen_blkif *blkif, struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400470{
471 unsigned long flags;
472 int was_empty;
473
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200474 spin_lock_irqsave(&blkif->pending_free_lock, flags);
475 was_empty = list_empty(&blkif->pending_free);
476 list_add(&req->free_list, &blkif->pending_free);
477 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400478 if (was_empty)
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200479 wake_up(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400480}
481
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400482/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400483 * Routines for managing virtual block devices (vbds).
484 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400485static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
486 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400487{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400488 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400489 int rc = -EACCES;
490
491 if ((operation != READ) && vbd->readonly)
492 goto out;
493
Jan Beulich8ab52152011-05-17 11:07:05 +0100494 if (likely(req->nr_sects)) {
495 blkif_sector_t end = req->sector_number + req->nr_sects;
496
497 if (unlikely(end < req->sector_number))
498 goto out;
499 if (unlikely(end > vbd_sz(vbd)))
500 goto out;
501 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400502
503 req->dev = vbd->pdevice;
504 req->bdev = vbd->bdev;
505 rc = 0;
506
507 out:
508 return rc;
509}
510
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400511static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400512{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400513 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400514 struct xenbus_transaction xbt;
515 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400516 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400517 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400518
Tao Chen77387b82015-04-01 15:04:22 +0000519 pr_info("VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400520 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Tao Chen77387b82015-04-01 15:04:22 +0000521 pr_info("VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400522 vbd->size = new_size;
523again:
524 err = xenbus_transaction_start(&xbt);
525 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000526 pr_warn("Error starting transaction\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400527 return;
528 }
529 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400530 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400531 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000532 pr_warn("Error writing new size\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400533 goto abort;
534 }
535 /*
536 * Write the current state; we will use this to synchronize
537 * the front-end. If the current state is "connected" the
538 * front-end will get the new size information online.
539 */
540 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
541 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000542 pr_warn("Error writing the state\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400543 goto abort;
544 }
545
546 err = xenbus_transaction_end(xbt, 0);
547 if (err == -EAGAIN)
548 goto again;
549 if (err)
Tao Chen77387b82015-04-01 15:04:22 +0000550 pr_warn("Error ending transaction\n");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400551 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400552abort:
553 xenbus_transaction_end(xbt, 1);
554}
555
556/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400557 * Notification from the guest OS.
558 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400559static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400560{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400561 blkif->waiting_reqs = 1;
562 wake_up(&blkif->wq);
563}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400564
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400565irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400566{
567 blkif_notify_work(dev_id);
568 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400569}
570
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400571/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400572 * SCHEDULER FUNCTIONS
573 */
574
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400575static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400576{
Tao Chen77387b82015-04-01 15:04:22 +0000577 pr_info("(%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200578 " | ds %4llu | pg: %4u/%4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400579 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800580 blkif->st_rd_req, blkif->st_wr_req,
Roger Pau Monnec1a15d02013-04-17 20:18:55 +0200581 blkif->st_f_req, blkif->st_ds_req,
582 blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200583 xen_blkif_max_pgrants);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400584 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
585 blkif->st_rd_req = 0;
586 blkif->st_wr_req = 0;
587 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800588 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400589}
590
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400591int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400592{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400593 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400594 struct xen_vbd *vbd = &blkif->vbd;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200595 unsigned long timeout;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500596 int ret;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400597
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400598 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400599
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400600 while (!kthread_should_stop()) {
601 if (try_to_freeze())
602 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400603 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400604 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400605
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200606 timeout = msecs_to_jiffies(LRU_INTERVAL);
607
608 timeout = wait_event_interruptible_timeout(
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400609 blkif->wq,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200610 blkif->waiting_reqs || kthread_should_stop(),
611 timeout);
612 if (timeout == 0)
613 goto purge_gnt_list;
614 timeout = wait_event_interruptible_timeout(
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200615 blkif->pending_free_wq,
616 !list_empty(&blkif->pending_free) ||
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200617 kthread_should_stop(),
618 timeout);
619 if (timeout == 0)
620 goto purge_gnt_list;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400621
622 blkif->waiting_reqs = 0;
623 smp_mb(); /* clear flag *before* checking for work */
624
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500625 ret = do_block_io_op(blkif);
626 if (ret > 0)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400627 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500628 if (ret == -EACCES)
629 wait_event_interruptible(blkif->shutdown_wq,
630 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400631
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200632purge_gnt_list:
633 if (blkif->vbd.feature_gnt_persistent &&
634 time_after(jiffies, blkif->next_lru)) {
635 purge_persistent_gnt(blkif);
636 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
637 }
638
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200639 /* Shrink if we have more than xen_blkif_max_buffer_pages */
640 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
641
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400642 if (log_stats && time_after(jiffies, blkif->st_print))
643 print_stats(blkif);
644 }
645
Roger Pau Monneef753412014-02-04 11:26:13 +0100646 /* Drain pending purge work */
647 flush_work(&blkif->persistent_purge_work);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200648
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400649 if (log_stats)
650 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400651
652 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400653 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400654
655 return 0;
656}
657
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400658/*
Roger Pau Monneef753412014-02-04 11:26:13 +0100659 * Remove persistent grants and empty the pool of free pages
660 */
661void xen_blkbk_free_caches(struct xen_blkif *blkif)
662{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400663 /* Free all persistent grant pages */
664 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
665 free_persistent_gnts(blkif, &blkif->persistent_gnts,
666 blkif->persistent_gnt_c);
667
668 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
669 blkif->persistent_gnt_c = 0;
670
Matt Rushton2ed22e32014-02-04 11:26:12 +0100671 /* Since we are shutting down remove all pages from the buffer */
672 shrink_free_pagepool(blkif, 0 /* All */);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400673}
674
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000675static unsigned int xen_blkbk_unmap_prepare(
676 struct xen_blkif *blkif,
677 struct grant_page **pages,
678 unsigned int num,
679 struct gnttab_unmap_grant_ref *unmap_ops,
680 struct page **unmap_pages)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400681{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400682 unsigned int i, invcount = 0;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400683
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200684 for (i = 0; i < num; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200685 if (pages[i]->persistent_gnt != NULL) {
686 put_persistent_gnt(blkif, pages[i]->persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200687 continue;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200688 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200689 if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400690 continue;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200691 unmap_pages[invcount] = pages[i]->page;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000692 gnttab_set_unmap_op(&unmap_ops[invcount], vaddr(pages[i]->page),
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200693 GNTMAP_host_map, pages[i]->handle);
694 pages[i]->handle = BLKBACK_INVALID_HANDLE;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000695 invcount++;
696 }
697
698 return invcount;
699}
700
701static void xen_blkbk_unmap_and_respond_callback(int result, struct gntab_unmap_queue_data *data)
702{
703 struct pending_req* pending_req = (struct pending_req*) (data->data);
704 struct xen_blkif *blkif = pending_req->blkif;
705
706 /* BUG_ON used to reproduce existing behaviour,
707 but is this the best way to deal with this? */
708 BUG_ON(result);
709
710 put_free_pages(blkif, data->pages, data->count);
711 make_response(blkif, pending_req->id,
712 pending_req->operation, pending_req->status);
713 free_req(blkif, pending_req);
714 /*
715 * Make sure the request is freed before releasing blkif,
716 * or there could be a race between free_req and the
717 * cleanup done in xen_blkif_free during shutdown.
718 *
719 * NB: The fact that we might try to wake up pending_free_wq
720 * before drain_complete (in case there's a drain going on)
721 * it's not a problem with our current implementation
722 * because we can assure there's no thread waiting on
723 * pending_free_wq if there's a drain going on, but it has
724 * to be taken into account if the current model is changed.
725 */
726 if (atomic_dec_and_test(&blkif->inflight) && atomic_read(&blkif->drain)) {
727 complete(&blkif->drain_complete);
728 }
729 xen_blkif_put(blkif);
730}
731
732static void xen_blkbk_unmap_and_respond(struct pending_req *req)
733{
734 struct gntab_unmap_queue_data* work = &req->gnttab_unmap_data;
735 struct xen_blkif *blkif = req->blkif;
736 struct grant_page **pages = req->segments;
737 unsigned int invcount;
738
Julien Grall6684fa12015-06-17 15:28:08 +0100739 invcount = xen_blkbk_unmap_prepare(blkif, pages, req->nr_segs,
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000740 req->unmap, req->unmap_pages);
741
742 work->data = req;
743 work->done = xen_blkbk_unmap_and_respond_callback;
744 work->unmap_ops = req->unmap;
745 work->kunmap_ops = NULL;
746 work->pages = req->unmap_pages;
747 work->count = invcount;
748
749 gnttab_unmap_refs_async(&req->gnttab_unmap_data);
750}
751
752
753/*
754 * Unmap the grant references.
755 *
756 * This could accumulate ops up to the batch size to reduce the number
757 * of hypercalls, but since this is only used in error paths there's
758 * no real need.
759 */
760static void xen_blkbk_unmap(struct xen_blkif *blkif,
761 struct grant_page *pages[],
762 int num)
763{
764 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
765 struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
766 unsigned int invcount = 0;
767 int ret;
768
769 while (num) {
770 unsigned int batch = min(num, BLKIF_MAX_SEGMENTS_PER_REQUEST);
771
772 invcount = xen_blkbk_unmap_prepare(blkif, pages, batch,
773 unmap, unmap_pages);
774 if (invcount) {
775 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200776 BUG_ON(ret);
777 put_free_pages(blkif, unmap_pages, invcount);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200778 }
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000779 pages += batch;
780 num -= batch;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200781 }
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400782}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400783
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200784static int xen_blkbk_map(struct xen_blkif *blkif,
785 struct grant_page *pages[],
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200786 int num, bool ro)
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400787{
788 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200789 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
790 struct persistent_gnt *persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200791 phys_addr_t addr = 0;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200792 int i, seg_idx, new_map_idx;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200793 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400794 int ret = 0;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200795 int last_map = 0, map_until = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200796 int use_persistent_gnts;
797
798 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
799
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400800 /*
801 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400802 * assign map[..] with the PFN of the page in our domain with the
803 * corresponding grant reference for each page.
804 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200805again:
806 for (i = map_until; i < num; i++) {
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400807 uint32_t flags;
808
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200809 if (use_persistent_gnts)
810 persistent_gnt = get_persistent_gnt(
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200811 blkif,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200812 pages[i]->gref);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200813
814 if (persistent_gnt) {
815 /*
816 * We are using persistent grants and
817 * the grant is already mapped
818 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200819 pages[i]->page = persistent_gnt->page;
820 pages[i]->persistent_gnt = persistent_gnt;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200821 } else {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200822 if (get_free_page(blkif, &pages[i]->page))
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200823 goto out_of_memory;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200824 addr = vaddr(pages[i]->page);
825 pages_to_gnt[segs_to_map] = pages[i]->page;
826 pages[i]->persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200827 flags = GNTMAP_host_map;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200828 if (!use_persistent_gnts && ro)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200829 flags |= GNTMAP_readonly;
830 gnttab_set_map_op(&map[segs_to_map++], addr,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200831 flags, pages[i]->gref,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200832 blkif->domid);
833 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200834 map_until = i + 1;
835 if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
836 break;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400837 }
838
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200839 if (segs_to_map) {
840 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
841 BUG_ON(ret);
842 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400843
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400844 /*
845 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400846 * so that when we access vaddr(pending_req,i) it has the contents of
847 * the page from the other domain.
848 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200849 for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200850 if (!pages[seg_idx]->persistent_gnt) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200851 /* This is a newly mapped grant */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200852 BUG_ON(new_map_idx >= segs_to_map);
853 if (unlikely(map[new_map_idx].status != 0)) {
Tao Chen77387b82015-04-01 15:04:22 +0000854 pr_debug("invalid buffer -- could not remap it\n");
Roger Pau Monné61cecca2014-09-15 11:55:27 +0200855 put_free_pages(blkif, &pages[seg_idx]->page, 1);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200856 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200857 ret |= 1;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200858 goto next;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200859 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200860 pages[seg_idx]->handle = map[new_map_idx].handle;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200861 } else {
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200862 continue;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200863 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200864 if (use_persistent_gnts &&
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200865 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200866 /*
867 * We are using persistent grants, the grant is
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200868 * not mapped but we might have room for it.
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200869 */
870 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
871 GFP_KERNEL);
872 if (!persistent_gnt) {
873 /*
874 * If we don't have enough memory to
875 * allocate the persistent_gnt struct
876 * map this grant non-persistenly
877 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200878 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200879 }
880 persistent_gnt->gnt = map[new_map_idx].ref;
881 persistent_gnt->handle = map[new_map_idx].handle;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200882 persistent_gnt->page = pages[seg_idx]->page;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200883 if (add_persistent_gnt(blkif,
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200884 persistent_gnt)) {
885 kfree(persistent_gnt);
886 persistent_gnt = NULL;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200887 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200888 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200889 pages[seg_idx]->persistent_gnt = persistent_gnt;
Tao Chen77387b82015-04-01 15:04:22 +0000890 pr_debug("grant %u added to the tree of persistent grants, using %u/%u\n",
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200891 persistent_gnt->gnt, blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200892 xen_blkif_max_pgrants);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200893 goto next;
894 }
895 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
896 blkif->vbd.overflow_max_grants = 1;
Tao Chen77387b82015-04-01 15:04:22 +0000897 pr_debug("domain %u, device %#x is using maximum number of persistent grants\n",
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200898 blkif->domid, blkif->vbd.handle);
899 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200900 /*
901 * We could not map this grant persistently, so use it as
902 * a non-persistent grant.
903 */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200904next:
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200905 new_map_idx++;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400906 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200907 segs_to_map = 0;
908 last_map = map_until;
909 if (map_until != num)
910 goto again;
911
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400912 return ret;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200913
914out_of_memory:
Tao Chen77387b82015-04-01 15:04:22 +0000915 pr_alert("%s: out of memory\n", __func__);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200916 put_free_pages(blkif, pages_to_gnt, segs_to_map);
917 return -ENOMEM;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400918}
919
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200920static int xen_blkbk_map_seg(struct pending_req *pending_req)
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200921{
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200922 int rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200923
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200924 rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
Julien Grall6684fa12015-06-17 15:28:08 +0100925 pending_req->nr_segs,
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200926 (pending_req->operation != BLKIF_OP_READ));
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200927
928 return rc;
929}
930
931static int xen_blkbk_parse_indirect(struct blkif_request *req,
932 struct pending_req *pending_req,
933 struct seg_buf seg[],
934 struct phys_req *preq)
935{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200936 struct grant_page **pages = pending_req->indirect_pages;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200937 struct xen_blkif *blkif = pending_req->blkif;
938 int indirect_grefs, rc, n, nseg, i;
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100939 struct blkif_request_segment *segments = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200940
Julien Grall6684fa12015-06-17 15:28:08 +0100941 nseg = pending_req->nr_segs;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200942 indirect_grefs = INDIRECT_PAGES(nseg);
943 BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
944
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200945 for (i = 0; i < indirect_grefs; i++)
946 pages[i]->gref = req->u.indirect.indirect_grefs[i];
947
948 rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200949 if (rc)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200950 goto unmap;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200951
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200952 for (n = 0, i = 0; n < nseg; n++) {
953 if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
954 /* Map indirect segments */
955 if (segments)
956 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200957 segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200958 }
959 i = n % SEGS_PER_INDIRECT_FRAME;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200960 pending_req->segments[n]->gref = segments[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200961 seg[n].nsec = segments[i].last_sect -
962 segments[i].first_sect + 1;
963 seg[n].offset = (segments[i].first_sect << 9);
964 if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
965 (segments[i].last_sect < segments[i].first_sect)) {
966 rc = -EINVAL;
967 goto unmap;
968 }
969 preq->nr_sects += seg[n].nsec;
970 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200971
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200972unmap:
973 if (segments)
974 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200975 xen_blkbk_unmap(blkif, pages, indirect_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200976 return rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200977}
978
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400979static int dispatch_discard_io(struct xen_blkif *blkif,
980 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800981{
982 int err = 0;
983 int status = BLKIF_RSP_OKAY;
984 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400985 unsigned long secure;
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500986 struct phys_req preq;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800987
Vegard Nossumea5ec762013-09-05 13:00:14 +0200988 xen_blkif_get(blkif);
989
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500990 preq.sector_number = req->u.discard.sector_number;
991 preq.nr_sects = req->u.discard.nr_sectors;
992
993 err = xen_vbd_translate(&preq, blkif, WRITE);
994 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000995 pr_warn("access denied: DISCARD [%llu->%llu] on dev=%04x\n",
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500996 preq.sector_number,
997 preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
998 goto fail_response;
999 }
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001000 blkif->st_ds_req++;
1001
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -04001002 secure = (blkif->vbd.discard_secure &&
1003 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
1004 BLKDEV_DISCARD_SECURE : 0;
1005
1006 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
1007 req->u.discard.nr_sectors,
1008 GFP_KERNEL, secure);
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -05001009fail_response:
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001010 if (err == -EOPNOTSUPP) {
Tao Chen77387b82015-04-01 15:04:22 +00001011 pr_debug("discard op failed, not supported\n");
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001012 status = BLKIF_RSP_EOPNOTSUPP;
1013 } else if (err)
1014 status = BLKIF_RSP_ERROR;
1015
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001016 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001017 xen_blkif_put(blkif);
1018 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001019}
1020
David Vrabel0e367ae2013-03-07 17:32:01 +00001021static int dispatch_other_io(struct xen_blkif *blkif,
1022 struct blkif_request *req,
1023 struct pending_req *pending_req)
1024{
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001025 free_req(blkif, pending_req);
David Vrabel0e367ae2013-03-07 17:32:01 +00001026 make_response(blkif, req->u.other.id, req->operation,
1027 BLKIF_RSP_EOPNOTSUPP);
1028 return -EIO;
1029}
1030
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001031static void xen_blk_drain_io(struct xen_blkif *blkif)
1032{
1033 atomic_set(&blkif->drain, 1);
1034 do {
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001035 if (atomic_read(&blkif->inflight) == 0)
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -04001036 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001037 wait_for_completion_interruptible_timeout(
1038 &blkif->drain_complete, HZ);
1039
1040 if (!atomic_read(&blkif->drain))
1041 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001042 } while (!kthread_should_stop());
1043 atomic_set(&blkif->drain, 0);
1044}
1045
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -04001046/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001047 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001048 */
1049
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001050static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001051{
1052 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001053 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001054 (error == -EOPNOTSUPP)) {
Tao Chen77387b82015-04-01 15:04:22 +00001055 pr_debug("flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001056 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001057 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001058 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
1059 (error == -EOPNOTSUPP)) {
Tao Chen77387b82015-04-01 15:04:22 +00001060 pr_debug("write barrier op failed, not supported\n");
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001061 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
1062 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001063 } else if (error) {
Tao Chen77387b82015-04-01 15:04:22 +00001064 pr_debug("Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001065 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001066 pending_req->status = BLKIF_RSP_ERROR;
1067 }
1068
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001069 /*
1070 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001071 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001072 * the proper response on the ring.
1073 */
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +00001074 if (atomic_dec_and_test(&pending_req->pendcnt))
1075 xen_blkbk_unmap_and_respond(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001076}
1077
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001078/*
1079 * bio callback.
1080 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001081static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001082{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001083 __end_block_io_op(bio->bi_private, error);
1084 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001085}
1086
1087
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001088
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001089/*
1090 * Function to copy the from the ring buffer the 'struct blkif_request'
1091 * (which has the sectors we want, number of them, grant references, etc),
1092 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001093 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001094static int
1095__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001096{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001097 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1098 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001099 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001100 RING_IDX rc, rp;
1101 int more_to_do = 0;
1102
1103 rc = blk_rings->common.req_cons;
1104 rp = blk_rings->common.sring->req_prod;
1105 rmb(); /* Ensure we see queued requests up to 'rp'. */
1106
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -05001107 if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
1108 rc = blk_rings->common.rsp_prod_pvt;
Tao Chen77387b82015-04-01 15:04:22 +00001109 pr_warn("Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -05001110 rp, rc, rp - rc, blkif->vbd.pdevice);
1111 return -EACCES;
1112 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001113 while (rc != rp) {
1114
1115 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1116 break;
1117
Keir Fraser8270b452009-03-06 08:29:15 +00001118 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001119 more_to_do = 1;
1120 break;
1121 }
1122
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001123 pending_req = alloc_req(blkif);
Keir Fraser8270b452009-03-06 08:29:15 +00001124 if (NULL == pending_req) {
1125 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001126 more_to_do = 1;
1127 break;
1128 }
1129
1130 switch (blkif->blk_protocol) {
1131 case BLKIF_PROTOCOL_NATIVE:
1132 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1133 break;
1134 case BLKIF_PROTOCOL_X86_32:
1135 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1136 break;
1137 case BLKIF_PROTOCOL_X86_64:
1138 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1139 break;
1140 default:
1141 BUG();
1142 }
1143 blk_rings->common.req_cons = ++rc; /* before make_response() */
1144
1145 /* Apply all sanity checks to /private copy/ of request. */
1146 barrier();
David Vrabel0e367ae2013-03-07 17:32:01 +00001147
1148 switch (req.operation) {
1149 case BLKIF_OP_READ:
1150 case BLKIF_OP_WRITE:
1151 case BLKIF_OP_WRITE_BARRIER:
1152 case BLKIF_OP_FLUSH_DISKCACHE:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001153 case BLKIF_OP_INDIRECT:
David Vrabel0e367ae2013-03-07 17:32:01 +00001154 if (dispatch_rw_block_io(blkif, &req, pending_req))
1155 goto done;
1156 break;
1157 case BLKIF_OP_DISCARD:
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001158 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001159 if (dispatch_discard_io(blkif, &req))
David Vrabel0e367ae2013-03-07 17:32:01 +00001160 goto done;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001161 break;
David Vrabel0e367ae2013-03-07 17:32:01 +00001162 default:
1163 if (dispatch_other_io(blkif, &req, pending_req))
1164 goto done;
1165 break;
1166 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001167
1168 /* Yield point for this unbounded loop. */
1169 cond_resched();
1170 }
David Vrabel0e367ae2013-03-07 17:32:01 +00001171done:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001172 return more_to_do;
1173}
1174
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001175static int
1176do_block_io_op(struct xen_blkif *blkif)
1177{
1178 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1179 int more_to_do;
1180
1181 do {
1182 more_to_do = __do_block_io_op(blkif);
1183 if (more_to_do)
1184 break;
1185
1186 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1187 } while (more_to_do);
1188
1189 return more_to_do;
1190}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001191/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001192 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1193 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001194 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001195static int dispatch_rw_block_io(struct xen_blkif *blkif,
1196 struct blkif_request *req,
1197 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001198{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001199 struct phys_req preq;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001200 struct seg_buf *seg = pending_req->seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001201 unsigned int nseg;
1202 struct bio *bio = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001203 struct bio **biolist = pending_req->biolist;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001204 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001205 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001206 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001207 bool drain = false;
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001208 struct grant_page **pages = pending_req->segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001209 unsigned short req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001210
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001211 req_operation = req->operation == BLKIF_OP_INDIRECT ?
1212 req->u.indirect.indirect_op : req->operation;
1213 if ((req->operation == BLKIF_OP_INDIRECT) &&
1214 (req_operation != BLKIF_OP_READ) &&
1215 (req_operation != BLKIF_OP_WRITE)) {
Tao Chen77387b82015-04-01 15:04:22 +00001216 pr_debug("Invalid indirect operation (%u)\n", req_operation);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001217 goto fail_response;
1218 }
1219
1220 switch (req_operation) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001221 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001222 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001223 operation = READ;
1224 break;
1225 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001226 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -04001227 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001228 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001229 case BLKIF_OP_WRITE_BARRIER:
1230 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001231 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001232 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001233 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001234 break;
1235 default:
1236 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001237 goto fail_response;
1238 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001239 }
1240
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001241 /* Check that the number of segments is sane. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001242 nseg = req->operation == BLKIF_OP_INDIRECT ?
1243 req->u.indirect.nr_segments : req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001244
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001245 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001246 unlikely((req->operation != BLKIF_OP_INDIRECT) &&
1247 (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
1248 unlikely((req->operation == BLKIF_OP_INDIRECT) &&
1249 (nseg > MAX_INDIRECT_SEGMENTS))) {
Tao Chen77387b82015-04-01 15:04:22 +00001250 pr_debug("Bad number of segments in request (%d)\n", nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001251 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001252 goto fail_response;
1253 }
1254
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001255 preq.nr_sects = 0;
1256
1257 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001258 pending_req->id = req->u.rw.id;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001259 pending_req->operation = req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001260 pending_req->status = BLKIF_RSP_OKAY;
Julien Grall6684fa12015-06-17 15:28:08 +01001261 pending_req->nr_segs = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001262
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001263 if (req->operation != BLKIF_OP_INDIRECT) {
1264 preq.dev = req->u.rw.handle;
1265 preq.sector_number = req->u.rw.sector_number;
1266 for (i = 0; i < nseg; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001267 pages[i]->gref = req->u.rw.seg[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001268 seg[i].nsec = req->u.rw.seg[i].last_sect -
1269 req->u.rw.seg[i].first_sect + 1;
1270 seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
1271 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1272 (req->u.rw.seg[i].last_sect <
1273 req->u.rw.seg[i].first_sect))
1274 goto fail_response;
1275 preq.nr_sects += seg[i].nsec;
1276 }
1277 } else {
1278 preq.dev = req->u.indirect.handle;
1279 preq.sector_number = req->u.indirect.sector_number;
1280 if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001281 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001282 }
1283
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001284 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Tao Chen77387b82015-04-01 15:04:22 +00001285 pr_debug("access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001286 operation == READ ? "read" : "write",
1287 preq.sector_number,
Chen Ganga72d9002013-02-28 10:34:23 +08001288 preq.sector_number + preq.nr_sects,
1289 blkif->vbd.pdevice);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001290 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001291 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001292
1293 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001294 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001295 * is set there.
1296 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001297 for (i = 0; i < nseg; i++) {
1298 if (((int)preq.sector_number|(int)seg[i].nsec) &
1299 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Tao Chen77387b82015-04-01 15:04:22 +00001300 pr_debug("Misaligned I/O request from domain %d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001301 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001302 goto fail_response;
1303 }
1304 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001305
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001306 /* Wait on all outstanding I/O's and once that has been completed
1307 * issue the WRITE_FLUSH.
1308 */
1309 if (drain)
1310 xen_blk_drain_io(pending_req->blkif);
1311
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001312 /*
1313 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001314 * set gnttab_set_unmap_op on all of the grant references and perform
1315 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -04001316 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001317 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001318 if (xen_blkbk_map_seg(pending_req))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001319 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001320
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001321 /*
1322 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1323 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1324 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001325 xen_blkif_get(blkif);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001326 atomic_inc(&blkif->inflight);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001327
1328 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001329 while ((bio == NULL) ||
1330 (bio_add_page(bio,
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001331 pages[i]->page,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001332 seg[i].nsec << 9,
Roger Pau Monneffb1dab2013-03-18 17:49:32 +01001333 seg[i].offset) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001334
Roger Pau Monne1e0f7a22013-06-22 09:59:17 +02001335 int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1336 bio = bio_alloc(GFP_KERNEL, nr_iovecs);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001337 if (unlikely(bio == NULL))
1338 goto fail_put_bio;
1339
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001340 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001341 bio->bi_bdev = preq.bdev;
1342 bio->bi_private = pending_req;
1343 bio->bi_end_io = end_block_io_op;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001344 bio->bi_iter.bi_sector = preq.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001345 }
1346
1347 preq.sector_number += seg[i].nsec;
1348 }
1349
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001350 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001351 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001352 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -04001353
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001354 bio = bio_alloc(GFP_KERNEL, 0);
1355 if (unlikely(bio == NULL))
1356 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001357
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001358 biolist[nbio++] = bio;
1359 bio->bi_bdev = preq.bdev;
1360 bio->bi_private = pending_req;
1361 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001362 }
1363
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001364 atomic_set(&pending_req->pendcnt, nbio);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001365 blk_start_plug(&plug);
1366
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001367 for (i = 0; i < nbio; i++)
1368 submit_bio(operation, biolist[i]);
1369
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001370 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -04001371 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001372
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001373 if (operation == READ)
1374 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -04001375 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001376 blkif->st_wr_sect += preq.nr_sects;
1377
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001378 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001379
1380 fail_flush:
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001381 xen_blkbk_unmap(blkif, pending_req->segments,
Julien Grall6684fa12015-06-17 15:28:08 +01001382 pending_req->nr_segs);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001383 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001384 /* Haven't submitted any bio's yet. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001385 make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001386 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001387 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001388 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001389
1390 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001391 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001392 bio_put(biolist[i]);
Jan Beulich0e5e0982013-03-11 09:39:55 +00001393 atomic_set(&pending_req->pendcnt, 1);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001394 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001395 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001396 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001397}
1398
1399
1400
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001401/*
1402 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001403 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001404static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001405 unsigned short op, int st)
1406{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001407 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001408 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001409 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001410 int notify;
1411
1412 resp.id = id;
1413 resp.operation = op;
1414 resp.status = st;
1415
1416 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1417 /* Place on the response ring for the relevant domain. */
1418 switch (blkif->blk_protocol) {
1419 case BLKIF_PROTOCOL_NATIVE:
1420 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1421 &resp, sizeof(resp));
1422 break;
1423 case BLKIF_PROTOCOL_X86_32:
1424 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1425 &resp, sizeof(resp));
1426 break;
1427 case BLKIF_PROTOCOL_X86_64:
1428 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1429 &resp, sizeof(resp));
1430 break;
1431 default:
1432 BUG();
1433 }
1434 blk_rings->common.rsp_prod_pvt++;
1435 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001436 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001437 if (notify)
1438 notify_remote_via_irq(blkif->irq);
1439}
1440
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001441static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001442{
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001443 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001444
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001445 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001446 return -ENODEV;
1447
Bob Liu86839c52015-06-03 13:40:03 +08001448 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_PAGE_ORDER) {
1449 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
1450 xen_blkif_max_ring_order, XENBUS_MAX_RING_PAGE_ORDER);
1451 xen_blkif_max_ring_order = XENBUS_MAX_RING_PAGE_ORDER;
1452 }
1453
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001454 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001455 if (rc)
1456 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001457
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001458 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001459 if (rc)
1460 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001461
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001462 failed_init:
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001463 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001464}
1465
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001466module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001467
1468MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001469MODULE_ALIAS("xen-backend:vbd");