blob: 6d1cc3df2ac66706db6029dd1b368c043e7688ed [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/******************************************************************************
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04002 *
3 * Back-end of the driver for virtual block devices. This portion of the
4 * driver exports a 'unified' block-device interface that can be accessed
5 * by any operating system that implements a compatible front end. A
6 * reference front-end implementation can be found in:
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04007 * drivers/block/xen-blkfront.c
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04008 *
9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10 * Copyright (c) 2005, Christopher Clark
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 * IN THE SOFTWARE.
35 */
36
37#include <linux/spinlock.h>
38#include <linux/kthread.h>
39#include <linux/list.h>
40#include <linux/delay.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080041#include <linux/freezer.h>
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020042#include <linux/bitmap.h>
Jeremy Fitzhardingeafd91d02009-09-15 14:12:37 -070043
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080044#include <xen/events.h>
45#include <xen/page.h>
Stefano Stabellinie79affc2012-08-08 17:21:14 +000046#include <xen/xen.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080047#include <asm/xen/hypervisor.h>
48#include <asm/xen/hypercall.h>
Roger Pau Monne087ffec2013-02-14 11:12:09 +010049#include <xen/balloon.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040050#include "common.h"
51
52/*
53 * These are rather arbitrary. They are fairly large because adjacent requests
54 * pulled from a communication ring are quite likely to end up being part of
55 * the same scatter/gather request at the disc.
56 *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040057 * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040058 *
59 * This will increase the chances of being able to write whole tracks.
60 * 64 should be enough to keep us competitive with Linux.
61 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040062static int xen_blkif_reqs = 64;
63module_param_named(reqs, xen_blkif_reqs, int, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040064MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
65
66/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040067static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040068module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040069
70/*
71 * Each outstanding request that we've passed to the lower device layers has a
72 * 'pending_req' allocated to it. Each buffer_head that completes decrements
73 * the pendcnt towards zero. When it hits zero, the specified domain has a
74 * response queued for it, with the saved 'id' passed back.
75 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040076struct pending_req {
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040077 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040078 u64 id;
79 int nr_pages;
80 atomic_t pendcnt;
81 unsigned short operation;
82 int status;
83 struct list_head free_list;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020084 DECLARE_BITMAP(unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040085};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040086
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040087#define BLKBACK_INVALID_HANDLE (~0)
88
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050089struct xen_blkbk {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040090 struct pending_req *pending_reqs;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040091 /* List of all 'pending_req' available */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050092 struct list_head pending_free;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040093 /* And its spinlock. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050094 spinlock_t pending_free_lock;
95 wait_queue_head_t pending_free_wq;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040096 /* The list of all pages that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050097 struct page **pending_pages;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040098 /* And the grant handles that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050099 grant_handle_t *pending_grant_handles;
100};
101
102static struct xen_blkbk *blkbk;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400103
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400104/*
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200105 * Maximum number of grant pages that can be mapped in blkback.
106 * BLKIF_MAX_SEGMENTS_PER_REQUEST * RING_SIZE is the maximum number of
107 * pages that blkback will persistently map.
108 * Currently, this is:
109 * RING_SIZE = 32 (for all known ring types)
110 * BLKIF_MAX_SEGMENTS_PER_REQUEST = 11
111 * sizeof(struct persistent_gnt) = 48
112 * So the maximum memory used to store the grants is:
113 * 32 * 11 * 48 = 16896 bytes
114 */
115static inline unsigned int max_mapped_grant_pages(enum blkif_protocol protocol)
116{
117 switch (protocol) {
118 case BLKIF_PROTOCOL_NATIVE:
119 return __CONST_RING_SIZE(blkif, PAGE_SIZE) *
120 BLKIF_MAX_SEGMENTS_PER_REQUEST;
121 case BLKIF_PROTOCOL_X86_32:
122 return __CONST_RING_SIZE(blkif_x86_32, PAGE_SIZE) *
123 BLKIF_MAX_SEGMENTS_PER_REQUEST;
124 case BLKIF_PROTOCOL_X86_64:
125 return __CONST_RING_SIZE(blkif_x86_64, PAGE_SIZE) *
126 BLKIF_MAX_SEGMENTS_PER_REQUEST;
127 default:
128 BUG();
129 }
130 return 0;
131}
132
133
134/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400135 * Little helpful macro to figure out the index and virtual address of the
136 * pending_pages[..]. For each 'pending_req' we have have up to
137 * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400138 * 10 and would index in the pending_pages[..].
139 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400140static inline int vaddr_pagenr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400141{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400142 return (req - blkbk->pending_reqs) *
143 BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400144}
145
Jan Beulichefe08a32010-02-05 14:19:33 -0500146#define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
147
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400148static inline unsigned long vaddr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400149{
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500150 unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400151 return (unsigned long)pfn_to_kaddr(pfn);
152}
153
154#define pending_handle(_req, _seg) \
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500155 (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400156
157
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400158static int do_block_io_op(struct xen_blkif *blkif);
159static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400160 struct blkif_request *req,
161 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400162static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400163 unsigned short op, int st);
164
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100165#define foreach_grant_safe(pos, n, rbtree, node) \
166 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
167 (n) = rb_next(&(pos)->node); \
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200168 &(pos)->node != NULL; \
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100169 (pos) = container_of(n, typeof(*(pos)), node), \
170 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200171
172
173static void add_persistent_gnt(struct rb_root *root,
174 struct persistent_gnt *persistent_gnt)
175{
176 struct rb_node **new = &(root->rb_node), *parent = NULL;
177 struct persistent_gnt *this;
178
179 /* Figure out where to put new node */
180 while (*new) {
181 this = container_of(*new, struct persistent_gnt, node);
182
183 parent = *new;
184 if (persistent_gnt->gnt < this->gnt)
185 new = &((*new)->rb_left);
186 else if (persistent_gnt->gnt > this->gnt)
187 new = &((*new)->rb_right);
188 else {
189 pr_alert(DRV_PFX " trying to add a gref that's already in the tree\n");
190 BUG();
191 }
192 }
193
194 /* Add new node and rebalance tree. */
195 rb_link_node(&(persistent_gnt->node), parent, new);
196 rb_insert_color(&(persistent_gnt->node), root);
197}
198
199static struct persistent_gnt *get_persistent_gnt(struct rb_root *root,
200 grant_ref_t gref)
201{
202 struct persistent_gnt *data;
203 struct rb_node *node = root->rb_node;
204
205 while (node) {
206 data = container_of(node, struct persistent_gnt, node);
207
208 if (gref < data->gnt)
209 node = node->rb_left;
210 else if (gref > data->gnt)
211 node = node->rb_right;
212 else
213 return data;
214 }
215 return NULL;
216}
217
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100218static void free_persistent_gnts(struct rb_root *root, unsigned int num)
219{
220 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
221 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
222 struct persistent_gnt *persistent_gnt;
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100223 struct rb_node *n;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100224 int ret = 0;
225 int segs_to_unmap = 0;
226
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100227 foreach_grant_safe(persistent_gnt, n, root, node) {
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100228 BUG_ON(persistent_gnt->handle ==
229 BLKBACK_INVALID_HANDLE);
230 gnttab_set_unmap_op(&unmap[segs_to_unmap],
231 (unsigned long) pfn_to_kaddr(page_to_pfn(
232 persistent_gnt->page)),
233 GNTMAP_host_map,
234 persistent_gnt->handle);
235
236 pages[segs_to_unmap] = persistent_gnt->page;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100237
238 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
239 !rb_next(&persistent_gnt->node)) {
240 ret = gnttab_unmap_refs(unmap, NULL, pages,
241 segs_to_unmap);
242 BUG_ON(ret);
Roger Pau Monne087ffec2013-02-14 11:12:09 +0100243 free_xenballooned_pages(segs_to_unmap, pages);
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100244 segs_to_unmap = 0;
245 }
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100246
247 rb_erase(&persistent_gnt->node, root);
248 kfree(persistent_gnt);
249 num--;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100250 }
251 BUG_ON(num != 0);
252}
253
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400254/*
255 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400256 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400257static struct pending_req *alloc_req(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400258{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400259 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400260 unsigned long flags;
261
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500262 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
263 if (!list_empty(&blkbk->pending_free)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400264 req = list_entry(blkbk->pending_free.next, struct pending_req,
265 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400266 list_del(&req->free_list);
267 }
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500268 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400269 return req;
270}
271
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400272/*
273 * Return the 'pending_req' structure back to the freepool. We also
274 * wake up the thread if it was waiting for a free page.
275 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400276static void free_req(struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400277{
278 unsigned long flags;
279 int was_empty;
280
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500281 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
282 was_empty = list_empty(&blkbk->pending_free);
283 list_add(&req->free_list, &blkbk->pending_free);
284 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400285 if (was_empty)
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500286 wake_up(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400287}
288
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400289/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400290 * Routines for managing virtual block devices (vbds).
291 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400292static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
293 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400294{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400295 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400296 int rc = -EACCES;
297
298 if ((operation != READ) && vbd->readonly)
299 goto out;
300
Jan Beulich8ab52152011-05-17 11:07:05 +0100301 if (likely(req->nr_sects)) {
302 blkif_sector_t end = req->sector_number + req->nr_sects;
303
304 if (unlikely(end < req->sector_number))
305 goto out;
306 if (unlikely(end > vbd_sz(vbd)))
307 goto out;
308 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400309
310 req->dev = vbd->pdevice;
311 req->bdev = vbd->bdev;
312 rc = 0;
313
314 out:
315 return rc;
316}
317
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400318static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400319{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400320 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400321 struct xenbus_transaction xbt;
322 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400323 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400324 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400325
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400326 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400327 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400328 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400329 vbd->size = new_size;
330again:
331 err = xenbus_transaction_start(&xbt);
332 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400333 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400334 return;
335 }
336 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400337 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400338 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400339 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400340 goto abort;
341 }
342 /*
343 * Write the current state; we will use this to synchronize
344 * the front-end. If the current state is "connected" the
345 * front-end will get the new size information online.
346 */
347 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
348 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400349 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400350 goto abort;
351 }
352
353 err = xenbus_transaction_end(xbt, 0);
354 if (err == -EAGAIN)
355 goto again;
356 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400357 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400358 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400359abort:
360 xenbus_transaction_end(xbt, 1);
361}
362
363/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400364 * Notification from the guest OS.
365 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400366static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400367{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400368 blkif->waiting_reqs = 1;
369 wake_up(&blkif->wq);
370}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400371
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400372irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400373{
374 blkif_notify_work(dev_id);
375 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400376}
377
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400378/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400379 * SCHEDULER FUNCTIONS
380 */
381
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400382static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400383{
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800384 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
385 " | ds %4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400386 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800387 blkif->st_rd_req, blkif->st_wr_req,
388 blkif->st_f_req, blkif->st_ds_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400389 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
390 blkif->st_rd_req = 0;
391 blkif->st_wr_req = 0;
392 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800393 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400394}
395
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400396int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400397{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400398 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400399 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400400
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400401 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400402
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400403 while (!kthread_should_stop()) {
404 if (try_to_freeze())
405 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400406 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400407 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400408
409 wait_event_interruptible(
410 blkif->wq,
411 blkif->waiting_reqs || kthread_should_stop());
412 wait_event_interruptible(
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500413 blkbk->pending_free_wq,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400414 !list_empty(&blkbk->pending_free) ||
415 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400416
417 blkif->waiting_reqs = 0;
418 smp_mb(); /* clear flag *before* checking for work */
419
420 if (do_block_io_op(blkif))
421 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400422
423 if (log_stats && time_after(jiffies, blkif->st_print))
424 print_stats(blkif);
425 }
426
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200427 /* Free all persistent grant pages */
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100428 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
429 free_persistent_gnts(&blkif->persistent_gnts,
430 blkif->persistent_gnt_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200431
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200432 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100433 blkif->persistent_gnt_c = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200434
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400435 if (log_stats)
436 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400437
438 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400439 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400440
441 return 0;
442}
443
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400444struct seg_buf {
445 unsigned long buf;
446 unsigned int nsec;
447};
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400448/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400449 * Unmap the grant references, and also remove the M2P over-rides
450 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400451 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400452static void xen_blkbk_unmap(struct pending_req *req)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400453{
454 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500455 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400456 unsigned int i, invcount = 0;
457 grant_handle_t handle;
458 int ret;
459
460 for (i = 0; i < req->nr_pages; i++) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200461 if (!test_bit(i, req->unmap_seg))
462 continue;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400463 handle = pending_handle(req, i);
464 if (handle == BLKBACK_INVALID_HANDLE)
465 continue;
466 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
467 GNTMAP_host_map, handle);
468 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500469 pages[invcount] = virt_to_page(vaddr(req, i));
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400470 invcount++;
471 }
472
Stefano Stabellini2fc136e2012-09-12 12:44:30 +0100473 ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400474 BUG_ON(ret);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400475}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400476
477static int xen_blkbk_map(struct blkif_request *req,
478 struct pending_req *pending_req,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200479 struct seg_buf seg[],
480 struct page *pages[])
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400481{
482 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200483 struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
484 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
485 struct persistent_gnt *persistent_gnt = NULL;
486 struct xen_blkif *blkif = pending_req->blkif;
487 phys_addr_t addr = 0;
488 int i, j;
489 bool new_map;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400490 int nseg = req->u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200491 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400492 int ret = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200493 int use_persistent_gnts;
494
495 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
496
497 BUG_ON(blkif->persistent_gnt_c >
498 max_mapped_grant_pages(pending_req->blkif->blk_protocol));
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400499
500 /*
501 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400502 * assign map[..] with the PFN of the page in our domain with the
503 * corresponding grant reference for each page.
504 */
505 for (i = 0; i < nseg; i++) {
506 uint32_t flags;
507
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200508 if (use_persistent_gnts)
509 persistent_gnt = get_persistent_gnt(
510 &blkif->persistent_gnts,
511 req->u.rw.seg[i].gref);
512
513 if (persistent_gnt) {
514 /*
515 * We are using persistent grants and
516 * the grant is already mapped
517 */
518 new_map = false;
519 } else if (use_persistent_gnts &&
520 blkif->persistent_gnt_c <
521 max_mapped_grant_pages(blkif->blk_protocol)) {
522 /*
523 * We are using persistent grants, the grant is
524 * not mapped but we have room for it
525 */
526 new_map = true;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100527 persistent_gnt = kmalloc(
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200528 sizeof(struct persistent_gnt),
529 GFP_KERNEL);
530 if (!persistent_gnt)
531 return -ENOMEM;
Roger Pau Monne087ffec2013-02-14 11:12:09 +0100532 if (alloc_xenballooned_pages(1, &persistent_gnt->page,
533 false)) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200534 kfree(persistent_gnt);
535 return -ENOMEM;
536 }
537 persistent_gnt->gnt = req->u.rw.seg[i].gref;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100538 persistent_gnt->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200539
540 pages_to_gnt[segs_to_map] =
541 persistent_gnt->page;
542 addr = (unsigned long) pfn_to_kaddr(
543 page_to_pfn(persistent_gnt->page));
544
545 add_persistent_gnt(&blkif->persistent_gnts,
546 persistent_gnt);
547 blkif->persistent_gnt_c++;
548 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
549 persistent_gnt->gnt, blkif->persistent_gnt_c,
550 max_mapped_grant_pages(blkif->blk_protocol));
551 } else {
552 /*
553 * We are either using persistent grants and
554 * hit the maximum limit of grants mapped,
555 * or we are not using persistent grants.
556 */
557 if (use_persistent_gnts &&
558 !blkif->vbd.overflow_max_grants) {
559 blkif->vbd.overflow_max_grants = 1;
560 pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
561 blkif->domid, blkif->vbd.handle);
562 }
563 new_map = true;
564 pages[i] = blkbk->pending_page(pending_req, i);
565 addr = vaddr(pending_req, i);
566 pages_to_gnt[segs_to_map] =
567 blkbk->pending_page(pending_req, i);
568 }
569
570 if (persistent_gnt) {
571 pages[i] = persistent_gnt->page;
572 persistent_gnts[i] = persistent_gnt;
573 } else {
574 persistent_gnts[i] = NULL;
575 }
576
577 if (new_map) {
578 flags = GNTMAP_host_map;
579 if (!persistent_gnt &&
580 (pending_req->operation != BLKIF_OP_READ))
581 flags |= GNTMAP_readonly;
582 gnttab_set_map_op(&map[segs_to_map++], addr,
583 flags, req->u.rw.seg[i].gref,
584 blkif->domid);
585 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400586 }
587
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200588 if (segs_to_map) {
589 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
590 BUG_ON(ret);
591 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400592
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400593 /*
594 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400595 * so that when we access vaddr(pending_req,i) it has the contents of
596 * the page from the other domain.
597 */
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200598 bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
599 for (i = 0, j = 0; i < nseg; i++) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100600 if (!persistent_gnts[i] ||
601 persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200602 /* This is a newly mapped grant */
603 BUG_ON(j >= segs_to_map);
604 if (unlikely(map[j].status != 0)) {
605 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
606 map[j].handle = BLKBACK_INVALID_HANDLE;
607 ret |= 1;
608 if (persistent_gnts[i]) {
609 rb_erase(&persistent_gnts[i]->node,
610 &blkif->persistent_gnts);
611 blkif->persistent_gnt_c--;
612 kfree(persistent_gnts[i]);
613 persistent_gnts[i] = NULL;
614 }
615 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400616 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200617 if (persistent_gnts[i]) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100618 if (persistent_gnts[i]->handle ==
619 BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200620 /*
621 * If this is a new persistent grant
622 * save the handler
623 */
624 persistent_gnts[i]->handle = map[j].handle;
625 persistent_gnts[i]->dev_bus_addr =
626 map[j++].dev_bus_addr;
627 }
628 pending_handle(pending_req, i) =
629 persistent_gnts[i]->handle;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400630
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200631 if (ret)
632 continue;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400633
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200634 seg[i].buf = persistent_gnts[i]->dev_bus_addr |
635 (req->u.rw.seg[i].first_sect << 9);
636 } else {
637 pending_handle(pending_req, i) = map[j].handle;
638 bitmap_set(pending_req->unmap_seg, i, 1);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400639
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200640 if (ret) {
641 j++;
642 continue;
643 }
644
645 seg[i].buf = map[j++].dev_bus_addr |
646 (req->u.rw.seg[i].first_sect << 9);
647 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400648 }
649 return ret;
650}
651
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400652static int dispatch_discard_io(struct xen_blkif *blkif,
653 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800654{
655 int err = 0;
656 int status = BLKIF_RSP_OKAY;
657 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400658 unsigned long secure;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800659
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400660 blkif->st_ds_req++;
661
662 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400663 secure = (blkif->vbd.discard_secure &&
664 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
665 BLKDEV_DISCARD_SECURE : 0;
666
667 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
668 req->u.discard.nr_sectors,
669 GFP_KERNEL, secure);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800670
671 if (err == -EOPNOTSUPP) {
672 pr_debug(DRV_PFX "discard op failed, not supported\n");
673 status = BLKIF_RSP_EOPNOTSUPP;
674 } else if (err)
675 status = BLKIF_RSP_ERROR;
676
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400677 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400678 xen_blkif_put(blkif);
679 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800680}
681
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400682static void xen_blk_drain_io(struct xen_blkif *blkif)
683{
684 atomic_set(&blkif->drain, 1);
685 do {
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -0400686 /* The initial value is one, and one refcnt taken at the
687 * start of the xen_blkif_schedule thread. */
688 if (atomic_read(&blkif->refcnt) <= 2)
689 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400690 wait_for_completion_interruptible_timeout(
691 &blkif->drain_complete, HZ);
692
693 if (!atomic_read(&blkif->drain))
694 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400695 } while (!kthread_should_stop());
696 atomic_set(&blkif->drain, 0);
697}
698
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400699/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400700 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400701 */
702
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400703static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400704{
705 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400706 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400707 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400708 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400709 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400710 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400711 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
712 (error == -EOPNOTSUPP)) {
713 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
714 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
715 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400716 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400717 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400718 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400719 pending_req->status = BLKIF_RSP_ERROR;
720 }
721
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400722 /*
723 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400724 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400725 * the proper response on the ring.
726 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400727 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400728 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400729 make_response(pending_req->blkif, pending_req->id,
730 pending_req->operation, pending_req->status);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400731 xen_blkif_put(pending_req->blkif);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400732 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
733 if (atomic_read(&pending_req->blkif->drain))
734 complete(&pending_req->blkif->drain_complete);
735 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400736 free_req(pending_req);
737 }
738}
739
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400740/*
741 * bio callback.
742 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800743static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400744{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400745 __end_block_io_op(bio->bi_private, error);
746 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400747}
748
749
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400750
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400751/*
752 * Function to copy the from the ring buffer the 'struct blkif_request'
753 * (which has the sectors we want, number of them, grant references, etc),
754 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400755 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700756static int
757__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400758{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800759 union blkif_back_rings *blk_rings = &blkif->blk_rings;
760 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400761 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400762 RING_IDX rc, rp;
763 int more_to_do = 0;
764
765 rc = blk_rings->common.req_cons;
766 rp = blk_rings->common.sring->req_prod;
767 rmb(); /* Ensure we see queued requests up to 'rp'. */
768
769 while (rc != rp) {
770
771 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
772 break;
773
Keir Fraser8270b452009-03-06 08:29:15 +0000774 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400775 more_to_do = 1;
776 break;
777 }
778
Keir Fraser8270b452009-03-06 08:29:15 +0000779 pending_req = alloc_req();
780 if (NULL == pending_req) {
781 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400782 more_to_do = 1;
783 break;
784 }
785
786 switch (blkif->blk_protocol) {
787 case BLKIF_PROTOCOL_NATIVE:
788 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
789 break;
790 case BLKIF_PROTOCOL_X86_32:
791 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
792 break;
793 case BLKIF_PROTOCOL_X86_64:
794 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
795 break;
796 default:
797 BUG();
798 }
799 blk_rings->common.req_cons = ++rc; /* before make_response() */
800
801 /* Apply all sanity checks to /private copy/ of request. */
802 barrier();
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400803 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
804 free_req(pending_req);
805 if (dispatch_discard_io(blkif, &req))
806 break;
807 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400808 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400809
810 /* Yield point for this unbounded loop. */
811 cond_resched();
812 }
813
814 return more_to_do;
815}
816
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700817static int
818do_block_io_op(struct xen_blkif *blkif)
819{
820 union blkif_back_rings *blk_rings = &blkif->blk_rings;
821 int more_to_do;
822
823 do {
824 more_to_do = __do_block_io_op(blkif);
825 if (more_to_do)
826 break;
827
828 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
829 } while (more_to_do);
830
831 return more_to_do;
832}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400833/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400834 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
835 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400836 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400837static int dispatch_rw_block_io(struct xen_blkif *blkif,
838 struct blkif_request *req,
839 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400840{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400841 struct phys_req preq;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400842 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400843 unsigned int nseg;
844 struct bio *bio = NULL;
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400845 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400846 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400847 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400848 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400849 bool drain = false;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200850 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400851
852 switch (req->operation) {
853 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400854 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400855 operation = READ;
856 break;
857 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400858 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -0400859 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400860 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400861 case BLKIF_OP_WRITE_BARRIER:
862 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400863 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400864 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400865 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400866 break;
867 default:
868 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400869 goto fail_response;
870 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400871 }
872
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400873 /* Check that the number of segments is sane. */
874 nseg = req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400875
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400876 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400877 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400878 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400879 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400880 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400881 goto fail_response;
882 }
883
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500884 preq.sector_number = req->u.rw.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400885 preq.nr_sects = 0;
886
887 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400888 pending_req->id = req->u.rw.id;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400889 pending_req->operation = req->operation;
890 pending_req->status = BLKIF_RSP_OKAY;
891 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400892
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400893 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500894 seg[i].nsec = req->u.rw.seg[i].last_sect -
895 req->u.rw.seg[i].first_sect + 1;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500896 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
897 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400898 goto fail_response;
899 preq.nr_sects += seg[i].nsec;
Konrad Rzeszutek Wilk976222e2011-04-15 11:38:29 -0400900
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400901 }
902
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400903 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400904 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400905 operation == READ ? "read" : "write",
906 preq.sector_number,
Chen Ganga72d9002013-02-28 10:34:23 +0800907 preq.sector_number + preq.nr_sects,
908 blkif->vbd.pdevice);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400909 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400910 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400911
912 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400913 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400914 * is set there.
915 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400916 for (i = 0; i < nseg; i++) {
917 if (((int)preq.sector_number|(int)seg[i].nsec) &
918 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400919 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400920 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400921 goto fail_response;
922 }
923 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400924
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400925 /* Wait on all outstanding I/O's and once that has been completed
926 * issue the WRITE_FLUSH.
927 */
928 if (drain)
929 xen_blk_drain_io(pending_req->blkif);
930
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400931 /*
932 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400933 * set gnttab_set_unmap_op on all of the grant references and perform
934 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400935 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400936 */
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200937 if (xen_blkbk_map(req, pending_req, seg, pages))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400938 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400939
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800940 /*
941 * This corresponding xen_blkif_put is done in __end_block_io_op, or
942 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
943 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400944 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400945
946 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400947 while ((bio == NULL) ||
948 (bio_add_page(bio,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200949 pages[i],
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400950 seg[i].nsec << 9,
951 seg[i].buf & ~PAGE_MASK) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400952
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400953 bio = bio_alloc(GFP_KERNEL, nseg-i);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400954 if (unlikely(bio == NULL))
955 goto fail_put_bio;
956
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400957 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400958 bio->bi_bdev = preq.bdev;
959 bio->bi_private = pending_req;
960 bio->bi_end_io = end_block_io_op;
961 bio->bi_sector = preq.sector_number;
962 }
963
964 preq.sector_number += seg[i].nsec;
965 }
966
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800967 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400968 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400969 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -0400970
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400971 bio = bio_alloc(GFP_KERNEL, 0);
972 if (unlikely(bio == NULL))
973 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400974
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400975 biolist[nbio++] = bio;
976 bio->bi_bdev = preq.bdev;
977 bio->bi_private = pending_req;
978 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400979 }
980
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400981 /*
982 * We set it one so that the last submit_bio does not have to call
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400983 * atomic_inc.
984 */
985 atomic_set(&pending_req->pendcnt, nbio);
986
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400987 /* Get a reference count for the disk queue and start sending I/O */
988 blk_start_plug(&plug);
989
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400990 for (i = 0; i < nbio; i++)
991 submit_bio(operation, biolist[i]);
992
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400993 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400994 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400995
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400996 if (operation == READ)
997 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -0400998 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400999 blkif->st_wr_sect += preq.nr_sects;
1000
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001001 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001002
1003 fail_flush:
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -04001004 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001005 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001006 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001007 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001008 free_req(pending_req);
1009 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001010 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001011
1012 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001013 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001014 bio_put(biolist[i]);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001015 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001016 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001017 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001018}
1019
1020
1021
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001022/*
1023 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001024 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001025static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001026 unsigned short op, int st)
1027{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001028 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001029 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001030 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001031 int notify;
1032
1033 resp.id = id;
1034 resp.operation = op;
1035 resp.status = st;
1036
1037 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1038 /* Place on the response ring for the relevant domain. */
1039 switch (blkif->blk_protocol) {
1040 case BLKIF_PROTOCOL_NATIVE:
1041 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1042 &resp, sizeof(resp));
1043 break;
1044 case BLKIF_PROTOCOL_X86_32:
1045 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1046 &resp, sizeof(resp));
1047 break;
1048 case BLKIF_PROTOCOL_X86_64:
1049 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1050 &resp, sizeof(resp));
1051 break;
1052 default:
1053 BUG();
1054 }
1055 blk_rings->common.rsp_prod_pvt++;
1056 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001057 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001058 if (notify)
1059 notify_remote_via_irq(blkif->irq);
1060}
1061
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001062static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001063{
1064 int i, mmap_pages;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001065 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001066
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001067 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001068 return -ENODEV;
1069
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001070 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001071 if (!blkbk) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001072 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001073 return -ENOMEM;
1074 }
1075
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001076 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001077
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001078 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001079 xen_blkif_reqs, GFP_KERNEL);
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001080 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001081 mmap_pages, GFP_KERNEL);
1082 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
1083 mmap_pages, GFP_KERNEL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001084
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001085 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
1086 !blkbk->pending_pages) {
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001087 rc = -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001088 goto out_of_memory;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001089 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001090
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001091 for (i = 0; i < mmap_pages; i++) {
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001092 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001093 blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001094 if (blkbk->pending_pages[i] == NULL) {
1095 rc = -ENOMEM;
1096 goto out_of_memory;
1097 }
1098 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001099 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001100 if (rc)
1101 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001102
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001103 INIT_LIST_HEAD(&blkbk->pending_free);
1104 spin_lock_init(&blkbk->pending_free_lock);
1105 init_waitqueue_head(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001106
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001107 for (i = 0; i < xen_blkif_reqs; i++)
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001108 list_add_tail(&blkbk->pending_reqs[i].free_list,
1109 &blkbk->pending_free);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001110
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001111 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001112 if (rc)
1113 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001114
1115 return 0;
1116
1117 out_of_memory:
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001118 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001119 failed_init:
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001120 kfree(blkbk->pending_reqs);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001121 kfree(blkbk->pending_grant_handles);
Dan Carpenter9b83c772011-05-27 09:27:16 +03001122 if (blkbk->pending_pages) {
1123 for (i = 0; i < mmap_pages; i++) {
1124 if (blkbk->pending_pages[i])
1125 __free_page(blkbk->pending_pages[i]);
1126 }
1127 kfree(blkbk->pending_pages);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001128 }
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001129 kfree(blkbk);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001130 blkbk = NULL;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001131 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001132}
1133
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001134module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001135
1136MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001137MODULE_ALIAS("xen-backend:vbd");