blob: 713fc9ff11492766efcb7a4795b4a1750ceb9707 [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/*
87 * The LRU mechanism to clean the lists of persistent grants needs to
88 * be executed periodically. The time interval between consecutive executions
89 * of the purge mechanism is set in ms.
90 */
91#define LRU_INTERVAL 100
92
93/*
94 * When the persistent grants list is full we will remove unused grants
95 * from the list. The percent number of grants to be removed at each LRU
96 * execution.
97 */
98#define LRU_PERCENT_CLEAN 5
99
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400100/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400101static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400102module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400103
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400104#define BLKBACK_INVALID_HANDLE (~0)
105
David Vrabelff4b1562015-01-08 18:06:01 +0000106/* Number of free pages to remove on each call to gnttab_free_pages */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200107#define NUM_BATCH_FREE_PAGES 10
108
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200109static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400110{
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200111 unsigned long flags;
112
113 spin_lock_irqsave(&blkif->free_pages_lock, flags);
114 if (list_empty(&blkif->free_pages)) {
115 BUG_ON(blkif->free_pages_num != 0);
116 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
David Vrabelff4b1562015-01-08 18:06:01 +0000117 return gnttab_alloc_pages(1, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200118 }
119 BUG_ON(blkif->free_pages_num == 0);
120 page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
121 list_del(&page[0]->lru);
122 blkif->free_pages_num--;
123 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
124
125 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400126}
127
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200128static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
129 int num)
130{
131 unsigned long flags;
132 int i;
133
134 spin_lock_irqsave(&blkif->free_pages_lock, flags);
135 for (i = 0; i < num; i++)
136 list_add(&page[i]->lru, &blkif->free_pages);
137 blkif->free_pages_num += num;
138 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
139}
140
141static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
142{
143 /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
144 struct page *page[NUM_BATCH_FREE_PAGES];
145 unsigned int num_pages = 0;
146 unsigned long flags;
147
148 spin_lock_irqsave(&blkif->free_pages_lock, flags);
149 while (blkif->free_pages_num > num) {
150 BUG_ON(list_empty(&blkif->free_pages));
151 page[num_pages] = list_first_entry(&blkif->free_pages,
152 struct page, lru);
153 list_del(&page[num_pages]->lru);
154 blkif->free_pages_num--;
155 if (++num_pages == NUM_BATCH_FREE_PAGES) {
156 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
David Vrabelff4b1562015-01-08 18:06:01 +0000157 gnttab_free_pages(num_pages, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200158 spin_lock_irqsave(&blkif->free_pages_lock, flags);
159 num_pages = 0;
160 }
161 }
162 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
163 if (num_pages != 0)
David Vrabelff4b1562015-01-08 18:06:01 +0000164 gnttab_free_pages(num_pages, page);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200165}
166
167#define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
168
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400169static int do_block_io_op(struct xen_blkif *blkif);
170static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400171 struct blkif_request *req,
172 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400173static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400174 unsigned short op, int st);
175
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100176#define foreach_grant_safe(pos, n, rbtree, node) \
177 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
Roger Pau Monne217fd5e2013-03-18 17:49:33 +0100178 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200179 &(pos)->node != NULL; \
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100180 (pos) = container_of(n, typeof(*(pos)), node), \
181 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200182
183
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200184/*
185 * We don't need locking around the persistent grant helpers
186 * because blkback uses a single-thread for each backed, so we
187 * can be sure that this functions will never be called recursively.
188 *
189 * The only exception to that is put_persistent_grant, that can be called
190 * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
191 * bit operations to modify the flags of a persistent grant and to count
192 * the number of used grants.
193 */
194static int add_persistent_gnt(struct xen_blkif *blkif,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200195 struct persistent_gnt *persistent_gnt)
196{
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200197 struct rb_node **new = NULL, *parent = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200198 struct persistent_gnt *this;
199
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200200 if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
201 if (!blkif->vbd.overflow_max_grants)
202 blkif->vbd.overflow_max_grants = 1;
203 return -EBUSY;
204 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200205 /* Figure out where to put new node */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200206 new = &blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200207 while (*new) {
208 this = container_of(*new, struct persistent_gnt, node);
209
210 parent = *new;
211 if (persistent_gnt->gnt < this->gnt)
212 new = &((*new)->rb_left);
213 else if (persistent_gnt->gnt > this->gnt)
214 new = &((*new)->rb_right);
215 else {
Tao Chen77387b82015-04-01 15:04:22 +0000216 pr_alert_ratelimited("trying to add a gref that's already in the tree\n");
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200217 return -EINVAL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200218 }
219 }
220
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200221 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
222 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200223 /* Add new node and rebalance tree. */
224 rb_link_node(&(persistent_gnt->node), parent, new);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200225 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
226 blkif->persistent_gnt_c++;
227 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200228 return 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200229}
230
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200231static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200232 grant_ref_t gref)
233{
234 struct persistent_gnt *data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200235 struct rb_node *node = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200236
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200237 node = blkif->persistent_gnts.rb_node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200238 while (node) {
239 data = container_of(node, struct persistent_gnt, node);
240
241 if (gref < data->gnt)
242 node = node->rb_left;
243 else if (gref > data->gnt)
244 node = node->rb_right;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200245 else {
246 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
Tao Chen77387b82015-04-01 15:04:22 +0000247 pr_alert_ratelimited("requesting a grant already in use\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200248 return NULL;
249 }
250 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
251 atomic_inc(&blkif->persistent_gnt_in_use);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200252 return data;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200253 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200254 }
255 return NULL;
256}
257
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200258static void put_persistent_gnt(struct xen_blkif *blkif,
259 struct persistent_gnt *persistent_gnt)
260{
261 if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
Tao Chen77387b82015-04-01 15:04:22 +0000262 pr_alert_ratelimited("freeing a grant already unused\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200263 set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
264 clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
265 atomic_dec(&blkif->persistent_gnt_in_use);
266}
267
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200268static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
269 unsigned int num)
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100270{
271 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
272 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
273 struct persistent_gnt *persistent_gnt;
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100274 struct rb_node *n;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100275 int segs_to_unmap = 0;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000276 struct gntab_unmap_queue_data unmap_data;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000277
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000278 unmap_data.pages = pages;
279 unmap_data.unmap_ops = unmap;
280 unmap_data.kunmap_ops = NULL;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100281
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100282 foreach_grant_safe(persistent_gnt, n, root, node) {
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100283 BUG_ON(persistent_gnt->handle ==
284 BLKBACK_INVALID_HANDLE);
285 gnttab_set_unmap_op(&unmap[segs_to_unmap],
286 (unsigned long) pfn_to_kaddr(page_to_pfn(
287 persistent_gnt->page)),
288 GNTMAP_host_map,
289 persistent_gnt->handle);
290
291 pages[segs_to_unmap] = persistent_gnt->page;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100292
293 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
294 !rb_next(&persistent_gnt->node)) {
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000295
296 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800297 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000298
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200299 put_free_pages(blkif, pages, segs_to_unmap);
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100300 segs_to_unmap = 0;
301 }
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100302
303 rb_erase(&persistent_gnt->node, root);
304 kfree(persistent_gnt);
305 num--;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100306 }
307 BUG_ON(num != 0);
308}
309
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700310void xen_blkbk_unmap_purged_grants(struct work_struct *work)
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200311{
312 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
313 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
314 struct persistent_gnt *persistent_gnt;
Bob Liu325d73b2015-04-03 14:42:58 +0800315 int segs_to_unmap = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200316 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
Bob Liu325d73b2015-04-03 14:42:58 +0800317 struct gntab_unmap_queue_data unmap_data;
Bob Liu325d73b2015-04-03 14:42:58 +0800318
Bob Liu325d73b2015-04-03 14:42:58 +0800319 unmap_data.pages = pages;
320 unmap_data.unmap_ops = unmap;
321 unmap_data.kunmap_ops = NULL;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200322
323 while(!list_empty(&blkif->persistent_purge_list)) {
324 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
325 struct persistent_gnt,
326 remove_node);
327 list_del(&persistent_gnt->remove_node);
328
329 gnttab_set_unmap_op(&unmap[segs_to_unmap],
330 vaddr(persistent_gnt->page),
331 GNTMAP_host_map,
332 persistent_gnt->handle);
333
334 pages[segs_to_unmap] = persistent_gnt->page;
335
336 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
Bob Liu325d73b2015-04-03 14:42:58 +0800337 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800338 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200339 put_free_pages(blkif, pages, segs_to_unmap);
340 segs_to_unmap = 0;
341 }
342 kfree(persistent_gnt);
343 }
344 if (segs_to_unmap > 0) {
Bob Liu325d73b2015-04-03 14:42:58 +0800345 unmap_data.count = segs_to_unmap;
Bob Liub44166c2015-04-03 14:42:59 +0800346 BUG_ON(gnttab_unmap_refs_sync(&unmap_data));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200347 put_free_pages(blkif, pages, segs_to_unmap);
348 }
349}
350
351static void purge_persistent_gnt(struct xen_blkif *blkif)
352{
353 struct persistent_gnt *persistent_gnt;
354 struct rb_node *n;
355 unsigned int num_clean, total;
Roger Pau Monne2d910542013-06-21 12:56:53 +0200356 bool scan_used = false, clean_used = false;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200357 struct rb_root *root;
358
359 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
360 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
361 !blkif->vbd.overflow_max_grants)) {
362 return;
363 }
364
365 if (work_pending(&blkif->persistent_purge_work)) {
Tao Chen77387b82015-04-01 15:04:22 +0000366 pr_alert_ratelimited("Scheduled work from previous purge is still pending, cannot purge list\n");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200367 return;
368 }
369
370 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
371 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
372 num_clean = min(blkif->persistent_gnt_c, num_clean);
Roger Pau Monne2d910542013-06-21 12:56:53 +0200373 if ((num_clean == 0) ||
374 (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200375 return;
376
377 /*
378 * At this point, we can assure that there will be no calls
379 * to get_persistent_grant (because we are executing this code from
380 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
381 * which means that the number of currently used grants will go down,
382 * but never up, so we will always be able to remove the requested
383 * number of grants.
384 */
385
386 total = num_clean;
387
Tao Chen77387b82015-04-01 15:04:22 +0000388 pr_debug("Going to purge %u persistent grants\n", num_clean);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200389
Roger Pau Monneef753412014-02-04 11:26:13 +0100390 BUG_ON(!list_empty(&blkif->persistent_purge_list));
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200391 root = &blkif->persistent_gnts;
392purge_list:
393 foreach_grant_safe(persistent_gnt, n, root, node) {
394 BUG_ON(persistent_gnt->handle ==
395 BLKBACK_INVALID_HANDLE);
396
Roger Pau Monne2d910542013-06-21 12:56:53 +0200397 if (clean_used) {
398 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
399 continue;
400 }
401
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200402 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
403 continue;
404 if (!scan_used &&
405 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
406 continue;
407
408 rb_erase(&persistent_gnt->node, root);
409 list_add(&persistent_gnt->remove_node,
410 &blkif->persistent_purge_list);
411 if (--num_clean == 0)
412 goto finished;
413 }
414 /*
415 * If we get here it means we also need to start cleaning
416 * grants that were used since last purge in order to cope
417 * with the requested num
418 */
Roger Pau Monne2d910542013-06-21 12:56:53 +0200419 if (!scan_used && !clean_used) {
Tao Chen77387b82015-04-01 15:04:22 +0000420 pr_debug("Still missing %u purged frames\n", num_clean);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200421 scan_used = true;
422 goto purge_list;
423 }
424finished:
Roger Pau Monne2d910542013-06-21 12:56:53 +0200425 if (!clean_used) {
Tao Chen77387b82015-04-01 15:04:22 +0000426 pr_debug("Finished scanning for grants to clean, removing used flag\n");
Roger Pau Monne2d910542013-06-21 12:56:53 +0200427 clean_used = true;
428 goto purge_list;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200429 }
Roger Pau Monne2d910542013-06-21 12:56:53 +0200430
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200431 blkif->persistent_gnt_c -= (total - num_clean);
432 blkif->vbd.overflow_max_grants = 0;
433
434 /* We can defer this work */
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200435 schedule_work(&blkif->persistent_purge_work);
Tao Chen77387b82015-04-01 15:04:22 +0000436 pr_debug("Purged %u/%u\n", (total - num_clean), total);
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200437 return;
438}
439
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400440/*
441 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400442 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200443static struct pending_req *alloc_req(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400444{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400445 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400446 unsigned long flags;
447
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200448 spin_lock_irqsave(&blkif->pending_free_lock, flags);
449 if (!list_empty(&blkif->pending_free)) {
450 req = list_entry(blkif->pending_free.next, struct pending_req,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400451 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400452 list_del(&req->free_list);
453 }
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200454 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400455 return req;
456}
457
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400458/*
459 * Return the 'pending_req' structure back to the freepool. We also
460 * wake up the thread if it was waiting for a free page.
461 */
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200462static void free_req(struct xen_blkif *blkif, struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400463{
464 unsigned long flags;
465 int was_empty;
466
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200467 spin_lock_irqsave(&blkif->pending_free_lock, flags);
468 was_empty = list_empty(&blkif->pending_free);
469 list_add(&req->free_list, &blkif->pending_free);
470 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400471 if (was_empty)
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200472 wake_up(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400473}
474
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400475/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400476 * Routines for managing virtual block devices (vbds).
477 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400478static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
479 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400480{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400481 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400482 int rc = -EACCES;
483
484 if ((operation != READ) && vbd->readonly)
485 goto out;
486
Jan Beulich8ab52152011-05-17 11:07:05 +0100487 if (likely(req->nr_sects)) {
488 blkif_sector_t end = req->sector_number + req->nr_sects;
489
490 if (unlikely(end < req->sector_number))
491 goto out;
492 if (unlikely(end > vbd_sz(vbd)))
493 goto out;
494 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400495
496 req->dev = vbd->pdevice;
497 req->bdev = vbd->bdev;
498 rc = 0;
499
500 out:
501 return rc;
502}
503
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400504static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400505{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400506 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400507 struct xenbus_transaction xbt;
508 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400509 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400510 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400511
Tao Chen77387b82015-04-01 15:04:22 +0000512 pr_info("VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400513 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Tao Chen77387b82015-04-01 15:04:22 +0000514 pr_info("VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400515 vbd->size = new_size;
516again:
517 err = xenbus_transaction_start(&xbt);
518 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000519 pr_warn("Error starting transaction\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400520 return;
521 }
522 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400523 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400524 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000525 pr_warn("Error writing new size\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400526 goto abort;
527 }
528 /*
529 * Write the current state; we will use this to synchronize
530 * the front-end. If the current state is "connected" the
531 * front-end will get the new size information online.
532 */
533 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
534 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000535 pr_warn("Error writing the state\n");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400536 goto abort;
537 }
538
539 err = xenbus_transaction_end(xbt, 0);
540 if (err == -EAGAIN)
541 goto again;
542 if (err)
Tao Chen77387b82015-04-01 15:04:22 +0000543 pr_warn("Error ending transaction\n");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400544 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400545abort:
546 xenbus_transaction_end(xbt, 1);
547}
548
549/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400550 * Notification from the guest OS.
551 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400552static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400553{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400554 blkif->waiting_reqs = 1;
555 wake_up(&blkif->wq);
556}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400557
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400558irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400559{
560 blkif_notify_work(dev_id);
561 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400562}
563
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400564/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400565 * SCHEDULER FUNCTIONS
566 */
567
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400568static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400569{
Tao Chen77387b82015-04-01 15:04:22 +0000570 pr_info("(%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200571 " | ds %4llu | pg: %4u/%4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400572 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800573 blkif->st_rd_req, blkif->st_wr_req,
Roger Pau Monnec1a15d02013-04-17 20:18:55 +0200574 blkif->st_f_req, blkif->st_ds_req,
575 blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200576 xen_blkif_max_pgrants);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400577 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
578 blkif->st_rd_req = 0;
579 blkif->st_wr_req = 0;
580 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800581 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400582}
583
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400584int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400585{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400586 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400587 struct xen_vbd *vbd = &blkif->vbd;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200588 unsigned long timeout;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500589 int ret;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400590
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400591 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400592
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400593 while (!kthread_should_stop()) {
594 if (try_to_freeze())
595 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400596 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400597 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400598
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200599 timeout = msecs_to_jiffies(LRU_INTERVAL);
600
601 timeout = wait_event_interruptible_timeout(
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400602 blkif->wq,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200603 blkif->waiting_reqs || kthread_should_stop(),
604 timeout);
605 if (timeout == 0)
606 goto purge_gnt_list;
607 timeout = wait_event_interruptible_timeout(
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200608 blkif->pending_free_wq,
609 !list_empty(&blkif->pending_free) ||
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200610 kthread_should_stop(),
611 timeout);
612 if (timeout == 0)
613 goto purge_gnt_list;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400614
615 blkif->waiting_reqs = 0;
616 smp_mb(); /* clear flag *before* checking for work */
617
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500618 ret = do_block_io_op(blkif);
619 if (ret > 0)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400620 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500621 if (ret == -EACCES)
622 wait_event_interruptible(blkif->shutdown_wq,
623 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400624
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200625purge_gnt_list:
626 if (blkif->vbd.feature_gnt_persistent &&
627 time_after(jiffies, blkif->next_lru)) {
628 purge_persistent_gnt(blkif);
629 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
630 }
631
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200632 /* Shrink if we have more than xen_blkif_max_buffer_pages */
633 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
634
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400635 if (log_stats && time_after(jiffies, blkif->st_print))
636 print_stats(blkif);
637 }
638
Roger Pau Monneef753412014-02-04 11:26:13 +0100639 /* Drain pending purge work */
640 flush_work(&blkif->persistent_purge_work);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200641
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400642 if (log_stats)
643 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400644
645 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400646 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647
648 return 0;
649}
650
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400651/*
Roger Pau Monneef753412014-02-04 11:26:13 +0100652 * Remove persistent grants and empty the pool of free pages
653 */
654void xen_blkbk_free_caches(struct xen_blkif *blkif)
655{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400656 /* Free all persistent grant pages */
657 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
658 free_persistent_gnts(blkif, &blkif->persistent_gnts,
659 blkif->persistent_gnt_c);
660
661 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
662 blkif->persistent_gnt_c = 0;
663
Matt Rushton2ed22e32014-02-04 11:26:12 +0100664 /* Since we are shutting down remove all pages from the buffer */
665 shrink_free_pagepool(blkif, 0 /* All */);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400666}
667
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000668static unsigned int xen_blkbk_unmap_prepare(
669 struct xen_blkif *blkif,
670 struct grant_page **pages,
671 unsigned int num,
672 struct gnttab_unmap_grant_ref *unmap_ops,
673 struct page **unmap_pages)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400674{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400675 unsigned int i, invcount = 0;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400676
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200677 for (i = 0; i < num; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200678 if (pages[i]->persistent_gnt != NULL) {
679 put_persistent_gnt(blkif, pages[i]->persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200680 continue;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200681 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200682 if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400683 continue;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200684 unmap_pages[invcount] = pages[i]->page;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000685 gnttab_set_unmap_op(&unmap_ops[invcount], vaddr(pages[i]->page),
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200686 GNTMAP_host_map, pages[i]->handle);
687 pages[i]->handle = BLKBACK_INVALID_HANDLE;
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000688 invcount++;
689 }
690
691 return invcount;
692}
693
694static void xen_blkbk_unmap_and_respond_callback(int result, struct gntab_unmap_queue_data *data)
695{
696 struct pending_req* pending_req = (struct pending_req*) (data->data);
697 struct xen_blkif *blkif = pending_req->blkif;
698
699 /* BUG_ON used to reproduce existing behaviour,
700 but is this the best way to deal with this? */
701 BUG_ON(result);
702
703 put_free_pages(blkif, data->pages, data->count);
704 make_response(blkif, pending_req->id,
705 pending_req->operation, pending_req->status);
706 free_req(blkif, pending_req);
707 /*
708 * Make sure the request is freed before releasing blkif,
709 * or there could be a race between free_req and the
710 * cleanup done in xen_blkif_free during shutdown.
711 *
712 * NB: The fact that we might try to wake up pending_free_wq
713 * before drain_complete (in case there's a drain going on)
714 * it's not a problem with our current implementation
715 * because we can assure there's no thread waiting on
716 * pending_free_wq if there's a drain going on, but it has
717 * to be taken into account if the current model is changed.
718 */
719 if (atomic_dec_and_test(&blkif->inflight) && atomic_read(&blkif->drain)) {
720 complete(&blkif->drain_complete);
721 }
722 xen_blkif_put(blkif);
723}
724
725static void xen_blkbk_unmap_and_respond(struct pending_req *req)
726{
727 struct gntab_unmap_queue_data* work = &req->gnttab_unmap_data;
728 struct xen_blkif *blkif = req->blkif;
729 struct grant_page **pages = req->segments;
730 unsigned int invcount;
731
732 invcount = xen_blkbk_unmap_prepare(blkif, pages, req->nr_pages,
733 req->unmap, req->unmap_pages);
734
735 work->data = req;
736 work->done = xen_blkbk_unmap_and_respond_callback;
737 work->unmap_ops = req->unmap;
738 work->kunmap_ops = NULL;
739 work->pages = req->unmap_pages;
740 work->count = invcount;
741
742 gnttab_unmap_refs_async(&req->gnttab_unmap_data);
743}
744
745
746/*
747 * Unmap the grant references.
748 *
749 * This could accumulate ops up to the batch size to reduce the number
750 * of hypercalls, but since this is only used in error paths there's
751 * no real need.
752 */
753static void xen_blkbk_unmap(struct xen_blkif *blkif,
754 struct grant_page *pages[],
755 int num)
756{
757 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
758 struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
759 unsigned int invcount = 0;
760 int ret;
761
762 while (num) {
763 unsigned int batch = min(num, BLKIF_MAX_SEGMENTS_PER_REQUEST);
764
765 invcount = xen_blkbk_unmap_prepare(blkif, pages, batch,
766 unmap, unmap_pages);
767 if (invcount) {
768 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200769 BUG_ON(ret);
770 put_free_pages(blkif, unmap_pages, invcount);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200771 }
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +0000772 pages += batch;
773 num -= batch;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200774 }
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400775}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400776
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200777static int xen_blkbk_map(struct xen_blkif *blkif,
778 struct grant_page *pages[],
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200779 int num, bool ro)
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400780{
781 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200782 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
783 struct persistent_gnt *persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200784 phys_addr_t addr = 0;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200785 int i, seg_idx, new_map_idx;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200786 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400787 int ret = 0;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200788 int last_map = 0, map_until = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200789 int use_persistent_gnts;
790
791 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
792
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400793 /*
794 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400795 * assign map[..] with the PFN of the page in our domain with the
796 * corresponding grant reference for each page.
797 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200798again:
799 for (i = map_until; i < num; i++) {
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400800 uint32_t flags;
801
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200802 if (use_persistent_gnts)
803 persistent_gnt = get_persistent_gnt(
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200804 blkif,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200805 pages[i]->gref);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200806
807 if (persistent_gnt) {
808 /*
809 * We are using persistent grants and
810 * the grant is already mapped
811 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200812 pages[i]->page = persistent_gnt->page;
813 pages[i]->persistent_gnt = persistent_gnt;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200814 } else {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200815 if (get_free_page(blkif, &pages[i]->page))
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200816 goto out_of_memory;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200817 addr = vaddr(pages[i]->page);
818 pages_to_gnt[segs_to_map] = pages[i]->page;
819 pages[i]->persistent_gnt = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200820 flags = GNTMAP_host_map;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200821 if (!use_persistent_gnts && ro)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200822 flags |= GNTMAP_readonly;
823 gnttab_set_map_op(&map[segs_to_map++], addr,
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200824 flags, pages[i]->gref,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200825 blkif->domid);
826 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200827 map_until = i + 1;
828 if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
829 break;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400830 }
831
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200832 if (segs_to_map) {
833 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
834 BUG_ON(ret);
835 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400836
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400837 /*
838 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400839 * so that when we access vaddr(pending_req,i) it has the contents of
840 * the page from the other domain.
841 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200842 for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200843 if (!pages[seg_idx]->persistent_gnt) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200844 /* This is a newly mapped grant */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200845 BUG_ON(new_map_idx >= segs_to_map);
846 if (unlikely(map[new_map_idx].status != 0)) {
Tao Chen77387b82015-04-01 15:04:22 +0000847 pr_debug("invalid buffer -- could not remap it\n");
Roger Pau Monné61cecca2014-09-15 11:55:27 +0200848 put_free_pages(blkif, &pages[seg_idx]->page, 1);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200849 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200850 ret |= 1;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200851 goto next;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200852 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200853 pages[seg_idx]->handle = map[new_map_idx].handle;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200854 } else {
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200855 continue;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200856 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200857 if (use_persistent_gnts &&
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200858 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200859 /*
860 * We are using persistent grants, the grant is
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200861 * not mapped but we might have room for it.
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200862 */
863 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
864 GFP_KERNEL);
865 if (!persistent_gnt) {
866 /*
867 * If we don't have enough memory to
868 * allocate the persistent_gnt struct
869 * map this grant non-persistenly
870 */
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200871 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200872 }
873 persistent_gnt->gnt = map[new_map_idx].ref;
874 persistent_gnt->handle = map[new_map_idx].handle;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200875 persistent_gnt->page = pages[seg_idx]->page;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200876 if (add_persistent_gnt(blkif,
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200877 persistent_gnt)) {
878 kfree(persistent_gnt);
879 persistent_gnt = NULL;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200880 goto next;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200881 }
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200882 pages[seg_idx]->persistent_gnt = persistent_gnt;
Tao Chen77387b82015-04-01 15:04:22 +0000883 pr_debug("grant %u added to the tree of persistent grants, using %u/%u\n",
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200884 persistent_gnt->gnt, blkif->persistent_gnt_c,
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200885 xen_blkif_max_pgrants);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200886 goto next;
887 }
888 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
889 blkif->vbd.overflow_max_grants = 1;
Tao Chen77387b82015-04-01 15:04:22 +0000890 pr_debug("domain %u, device %#x is using maximum number of persistent grants\n",
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200891 blkif->domid, blkif->vbd.handle);
892 }
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200893 /*
894 * We could not map this grant persistently, so use it as
895 * a non-persistent grant.
896 */
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200897next:
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200898 new_map_idx++;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400899 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200900 segs_to_map = 0;
901 last_map = map_until;
902 if (map_until != num)
903 goto again;
904
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400905 return ret;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200906
907out_of_memory:
Tao Chen77387b82015-04-01 15:04:22 +0000908 pr_alert("%s: out of memory\n", __func__);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200909 put_free_pages(blkif, pages_to_gnt, segs_to_map);
910 return -ENOMEM;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400911}
912
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200913static int xen_blkbk_map_seg(struct pending_req *pending_req)
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200914{
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200915 int rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200916
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200917 rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200918 pending_req->nr_pages,
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200919 (pending_req->operation != BLKIF_OP_READ));
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200920
921 return rc;
922}
923
924static int xen_blkbk_parse_indirect(struct blkif_request *req,
925 struct pending_req *pending_req,
926 struct seg_buf seg[],
927 struct phys_req *preq)
928{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200929 struct grant_page **pages = pending_req->indirect_pages;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200930 struct xen_blkif *blkif = pending_req->blkif;
931 int indirect_grefs, rc, n, nseg, i;
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100932 struct blkif_request_segment *segments = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200933
934 nseg = pending_req->nr_pages;
935 indirect_grefs = INDIRECT_PAGES(nseg);
936 BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
937
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200938 for (i = 0; i < indirect_grefs; i++)
939 pages[i]->gref = req->u.indirect.indirect_grefs[i];
940
941 rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200942 if (rc)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200943 goto unmap;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200944
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200945 for (n = 0, i = 0; n < nseg; n++) {
946 if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
947 /* Map indirect segments */
948 if (segments)
949 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200950 segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200951 }
952 i = n % SEGS_PER_INDIRECT_FRAME;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200953 pending_req->segments[n]->gref = segments[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200954 seg[n].nsec = segments[i].last_sect -
955 segments[i].first_sect + 1;
956 seg[n].offset = (segments[i].first_sect << 9);
957 if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
958 (segments[i].last_sect < segments[i].first_sect)) {
959 rc = -EINVAL;
960 goto unmap;
961 }
962 preq->nr_sects += seg[n].nsec;
963 }
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200964
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200965unmap:
966 if (segments)
967 kunmap_atomic(segments);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200968 xen_blkbk_unmap(blkif, pages, indirect_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200969 return rc;
Roger Pau Monne31552ee2013-04-17 20:19:00 +0200970}
971
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400972static int dispatch_discard_io(struct xen_blkif *blkif,
973 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800974{
975 int err = 0;
976 int status = BLKIF_RSP_OKAY;
977 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400978 unsigned long secure;
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500979 struct phys_req preq;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800980
Vegard Nossumea5ec762013-09-05 13:00:14 +0200981 xen_blkif_get(blkif);
982
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500983 preq.sector_number = req->u.discard.sector_number;
984 preq.nr_sects = req->u.discard.nr_sectors;
985
986 err = xen_vbd_translate(&preq, blkif, WRITE);
987 if (err) {
Tao Chen77387b82015-04-01 15:04:22 +0000988 pr_warn("access denied: DISCARD [%llu->%llu] on dev=%04x\n",
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -0500989 preq.sector_number,
990 preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
991 goto fail_response;
992 }
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400993 blkif->st_ds_req++;
994
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400995 secure = (blkif->vbd.discard_secure &&
996 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
997 BLKDEV_DISCARD_SECURE : 0;
998
999 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
1000 req->u.discard.nr_sectors,
1001 GFP_KERNEL, secure);
Konrad Rzeszutek Wilk604c4992013-01-16 11:33:52 -05001002fail_response:
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001003 if (err == -EOPNOTSUPP) {
Tao Chen77387b82015-04-01 15:04:22 +00001004 pr_debug("discard op failed, not supported\n");
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001005 status = BLKIF_RSP_EOPNOTSUPP;
1006 } else if (err)
1007 status = BLKIF_RSP_ERROR;
1008
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001009 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001010 xen_blkif_put(blkif);
1011 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001012}
1013
David Vrabel0e367ae2013-03-07 17:32:01 +00001014static int dispatch_other_io(struct xen_blkif *blkif,
1015 struct blkif_request *req,
1016 struct pending_req *pending_req)
1017{
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001018 free_req(blkif, pending_req);
David Vrabel0e367ae2013-03-07 17:32:01 +00001019 make_response(blkif, req->u.other.id, req->operation,
1020 BLKIF_RSP_EOPNOTSUPP);
1021 return -EIO;
1022}
1023
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001024static void xen_blk_drain_io(struct xen_blkif *blkif)
1025{
1026 atomic_set(&blkif->drain, 1);
1027 do {
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001028 if (atomic_read(&blkif->inflight) == 0)
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -04001029 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001030 wait_for_completion_interruptible_timeout(
1031 &blkif->drain_complete, HZ);
1032
1033 if (!atomic_read(&blkif->drain))
1034 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001035 } while (!kthread_should_stop());
1036 atomic_set(&blkif->drain, 0);
1037}
1038
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -04001039/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001040 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001041 */
1042
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001043static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001044{
1045 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001046 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001047 (error == -EOPNOTSUPP)) {
Tao Chen77387b82015-04-01 15:04:22 +00001048 pr_debug("flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001049 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001050 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001051 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
1052 (error == -EOPNOTSUPP)) {
Tao Chen77387b82015-04-01 15:04:22 +00001053 pr_debug("write barrier op failed, not supported\n");
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001054 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
1055 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001056 } else if (error) {
Tao Chen77387b82015-04-01 15:04:22 +00001057 pr_debug("Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001058 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001059 pending_req->status = BLKIF_RSP_ERROR;
1060 }
1061
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001062 /*
1063 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001064 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001065 * the proper response on the ring.
1066 */
Jennifer Herbertc43cf3e2015-01-05 16:49:22 +00001067 if (atomic_dec_and_test(&pending_req->pendcnt))
1068 xen_blkbk_unmap_and_respond(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001069}
1070
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001071/*
1072 * bio callback.
1073 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001074static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001075{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001076 __end_block_io_op(bio->bi_private, error);
1077 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001078}
1079
1080
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001081
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001082/*
1083 * Function to copy the from the ring buffer the 'struct blkif_request'
1084 * (which has the sectors we want, number of them, grant references, etc),
1085 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001086 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001087static int
1088__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001089{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001090 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1091 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001092 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001093 RING_IDX rc, rp;
1094 int more_to_do = 0;
1095
1096 rc = blk_rings->common.req_cons;
1097 rp = blk_rings->common.sring->req_prod;
1098 rmb(); /* Ensure we see queued requests up to 'rp'. */
1099
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -05001100 if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
1101 rc = blk_rings->common.rsp_prod_pvt;
Tao Chen77387b82015-04-01 15:04:22 +00001102 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 -05001103 rp, rc, rp - rc, blkif->vbd.pdevice);
1104 return -EACCES;
1105 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001106 while (rc != rp) {
1107
1108 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1109 break;
1110
Keir Fraser8270b452009-03-06 08:29:15 +00001111 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001112 more_to_do = 1;
1113 break;
1114 }
1115
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001116 pending_req = alloc_req(blkif);
Keir Fraser8270b452009-03-06 08:29:15 +00001117 if (NULL == pending_req) {
1118 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001119 more_to_do = 1;
1120 break;
1121 }
1122
1123 switch (blkif->blk_protocol) {
1124 case BLKIF_PROTOCOL_NATIVE:
1125 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1126 break;
1127 case BLKIF_PROTOCOL_X86_32:
1128 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1129 break;
1130 case BLKIF_PROTOCOL_X86_64:
1131 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1132 break;
1133 default:
1134 BUG();
1135 }
1136 blk_rings->common.req_cons = ++rc; /* before make_response() */
1137
1138 /* Apply all sanity checks to /private copy/ of request. */
1139 barrier();
David Vrabel0e367ae2013-03-07 17:32:01 +00001140
1141 switch (req.operation) {
1142 case BLKIF_OP_READ:
1143 case BLKIF_OP_WRITE:
1144 case BLKIF_OP_WRITE_BARRIER:
1145 case BLKIF_OP_FLUSH_DISKCACHE:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001146 case BLKIF_OP_INDIRECT:
David Vrabel0e367ae2013-03-07 17:32:01 +00001147 if (dispatch_rw_block_io(blkif, &req, pending_req))
1148 goto done;
1149 break;
1150 case BLKIF_OP_DISCARD:
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001151 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001152 if (dispatch_discard_io(blkif, &req))
David Vrabel0e367ae2013-03-07 17:32:01 +00001153 goto done;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001154 break;
David Vrabel0e367ae2013-03-07 17:32:01 +00001155 default:
1156 if (dispatch_other_io(blkif, &req, pending_req))
1157 goto done;
1158 break;
1159 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001160
1161 /* Yield point for this unbounded loop. */
1162 cond_resched();
1163 }
David Vrabel0e367ae2013-03-07 17:32:01 +00001164done:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001165 return more_to_do;
1166}
1167
Daniel Stoddenb4726a92011-05-28 13:21:10 -07001168static int
1169do_block_io_op(struct xen_blkif *blkif)
1170{
1171 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1172 int more_to_do;
1173
1174 do {
1175 more_to_do = __do_block_io_op(blkif);
1176 if (more_to_do)
1177 break;
1178
1179 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1180 } while (more_to_do);
1181
1182 return more_to_do;
1183}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001184/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001185 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1186 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001187 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001188static int dispatch_rw_block_io(struct xen_blkif *blkif,
1189 struct blkif_request *req,
1190 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001191{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001192 struct phys_req preq;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001193 struct seg_buf *seg = pending_req->seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001194 unsigned int nseg;
1195 struct bio *bio = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001196 struct bio **biolist = pending_req->biolist;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001197 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001198 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001199 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001200 bool drain = false;
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001201 struct grant_page **pages = pending_req->segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001202 unsigned short req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001203
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001204 req_operation = req->operation == BLKIF_OP_INDIRECT ?
1205 req->u.indirect.indirect_op : req->operation;
1206 if ((req->operation == BLKIF_OP_INDIRECT) &&
1207 (req_operation != BLKIF_OP_READ) &&
1208 (req_operation != BLKIF_OP_WRITE)) {
Tao Chen77387b82015-04-01 15:04:22 +00001209 pr_debug("Invalid indirect operation (%u)\n", req_operation);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001210 goto fail_response;
1211 }
1212
1213 switch (req_operation) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001214 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001215 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001216 operation = READ;
1217 break;
1218 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001219 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -04001220 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001221 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001222 case BLKIF_OP_WRITE_BARRIER:
1223 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001224 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001225 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -04001226 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001227 break;
1228 default:
1229 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001230 goto fail_response;
1231 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001232 }
1233
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001234 /* Check that the number of segments is sane. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001235 nseg = req->operation == BLKIF_OP_INDIRECT ?
1236 req->u.indirect.nr_segments : req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001237
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001238 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001239 unlikely((req->operation != BLKIF_OP_INDIRECT) &&
1240 (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
1241 unlikely((req->operation == BLKIF_OP_INDIRECT) &&
1242 (nseg > MAX_INDIRECT_SEGMENTS))) {
Tao Chen77387b82015-04-01 15:04:22 +00001243 pr_debug("Bad number of segments in request (%d)\n", nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001244 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001245 goto fail_response;
1246 }
1247
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001248 preq.nr_sects = 0;
1249
1250 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001251 pending_req->id = req->u.rw.id;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001252 pending_req->operation = req_operation;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001253 pending_req->status = BLKIF_RSP_OKAY;
1254 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001255
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001256 if (req->operation != BLKIF_OP_INDIRECT) {
1257 preq.dev = req->u.rw.handle;
1258 preq.sector_number = req->u.rw.sector_number;
1259 for (i = 0; i < nseg; i++) {
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001260 pages[i]->gref = req->u.rw.seg[i].gref;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001261 seg[i].nsec = req->u.rw.seg[i].last_sect -
1262 req->u.rw.seg[i].first_sect + 1;
1263 seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
1264 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1265 (req->u.rw.seg[i].last_sect <
1266 req->u.rw.seg[i].first_sect))
1267 goto fail_response;
1268 preq.nr_sects += seg[i].nsec;
1269 }
1270 } else {
1271 preq.dev = req->u.indirect.handle;
1272 preq.sector_number = req->u.indirect.sector_number;
1273 if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001274 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001275 }
1276
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001277 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Tao Chen77387b82015-04-01 15:04:22 +00001278 pr_debug("access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001279 operation == READ ? "read" : "write",
1280 preq.sector_number,
Chen Ganga72d9002013-02-28 10:34:23 +08001281 preq.sector_number + preq.nr_sects,
1282 blkif->vbd.pdevice);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001283 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001284 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001285
1286 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -04001287 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001288 * is set there.
1289 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001290 for (i = 0; i < nseg; i++) {
1291 if (((int)preq.sector_number|(int)seg[i].nsec) &
1292 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Tao Chen77387b82015-04-01 15:04:22 +00001293 pr_debug("Misaligned I/O request from domain %d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -04001294 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -04001295 goto fail_response;
1296 }
1297 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001298
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -04001299 /* Wait on all outstanding I/O's and once that has been completed
1300 * issue the WRITE_FLUSH.
1301 */
1302 if (drain)
1303 xen_blk_drain_io(pending_req->blkif);
1304
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -04001305 /*
1306 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001307 * set gnttab_set_unmap_op on all of the grant references and perform
1308 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -04001309 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001310 */
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001311 if (xen_blkbk_map_seg(pending_req))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -04001312 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001313
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001314 /*
1315 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1316 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1317 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001318 xen_blkif_get(blkif);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +01001319 atomic_inc(&blkif->inflight);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001320
1321 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001322 while ((bio == NULL) ||
1323 (bio_add_page(bio,
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001324 pages[i]->page,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001325 seg[i].nsec << 9,
Roger Pau Monneffb1dab2013-03-18 17:49:32 +01001326 seg[i].offset) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001327
Roger Pau Monne1e0f7a22013-06-22 09:59:17 +02001328 int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1329 bio = bio_alloc(GFP_KERNEL, nr_iovecs);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001330 if (unlikely(bio == NULL))
1331 goto fail_put_bio;
1332
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001333 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001334 bio->bi_bdev = preq.bdev;
1335 bio->bi_private = pending_req;
1336 bio->bi_end_io = end_block_io_op;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001337 bio->bi_iter.bi_sector = preq.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001338 }
1339
1340 preq.sector_number += seg[i].nsec;
1341 }
1342
Li Dongyangb3cb0d62011-09-01 18:39:10 +08001343 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001344 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001345 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -04001346
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001347 bio = bio_alloc(GFP_KERNEL, 0);
1348 if (unlikely(bio == NULL))
1349 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001350
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -04001351 biolist[nbio++] = bio;
1352 bio->bi_bdev = preq.bdev;
1353 bio->bi_private = pending_req;
1354 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001355 }
1356
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001357 atomic_set(&pending_req->pendcnt, nbio);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001358 blk_start_plug(&plug);
1359
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001360 for (i = 0; i < nbio; i++)
1361 submit_bio(operation, biolist[i]);
1362
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001363 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -04001364 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -04001365
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001366 if (operation == READ)
1367 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -04001368 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001369 blkif->st_wr_sect += preq.nr_sects;
1370
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001371 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001372
1373 fail_flush:
Roger Pau Monnebb642e82013-05-02 10:21:17 +02001374 xen_blkbk_unmap(blkif, pending_req->segments,
Roger Pau Monne31552ee2013-04-17 20:19:00 +02001375 pending_req->nr_pages);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001376 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001377 /* Haven't submitted any bio's yet. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001378 make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +02001379 free_req(blkif, pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001380 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001381 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001382
1383 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001384 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001385 bio_put(biolist[i]);
Jan Beulich0e5e0982013-03-11 09:39:55 +00001386 atomic_set(&pending_req->pendcnt, 1);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001387 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001388 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001389 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001390}
1391
1392
1393
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001394/*
1395 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001396 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001397static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001398 unsigned short op, int st)
1399{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001400 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001401 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001402 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001403 int notify;
1404
1405 resp.id = id;
1406 resp.operation = op;
1407 resp.status = st;
1408
1409 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1410 /* Place on the response ring for the relevant domain. */
1411 switch (blkif->blk_protocol) {
1412 case BLKIF_PROTOCOL_NATIVE:
1413 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1414 &resp, sizeof(resp));
1415 break;
1416 case BLKIF_PROTOCOL_X86_32:
1417 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1418 &resp, sizeof(resp));
1419 break;
1420 case BLKIF_PROTOCOL_X86_64:
1421 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1422 &resp, sizeof(resp));
1423 break;
1424 default:
1425 BUG();
1426 }
1427 blk_rings->common.rsp_prod_pvt++;
1428 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001429 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001430 if (notify)
1431 notify_remote_via_irq(blkif->irq);
1432}
1433
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001434static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001435{
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001436 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001437
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001438 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001439 return -ENODEV;
1440
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001441 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001442 if (rc)
1443 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001444
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001445 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001446 if (rc)
1447 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001448
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001449 failed_init:
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001450 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001451}
1452
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001453module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001454
1455MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001456MODULE_ALIAS("xen-backend:vbd");