blob: 1966a7cfd8ce9d5a4f582aa3656f87378f22f70c [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 Monne0a8704a52012-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>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040049#include "common.h"
50
51/*
52 * These are rather arbitrary. They are fairly large because adjacent requests
53 * pulled from a communication ring are quite likely to end up being part of
54 * the same scatter/gather request at the disc.
55 *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040056 * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040057 *
58 * This will increase the chances of being able to write whole tracks.
59 * 64 should be enough to keep us competitive with Linux.
60 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040061static int xen_blkif_reqs = 64;
62module_param_named(reqs, xen_blkif_reqs, int, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040063MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
64
65/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040066static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040067module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040068
69/*
70 * Each outstanding request that we've passed to the lower device layers has a
71 * 'pending_req' allocated to it. Each buffer_head that completes decrements
72 * the pendcnt towards zero. When it hits zero, the specified domain has a
73 * response queued for it, with the saved 'id' passed back.
74 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040075struct pending_req {
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040076 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040077 u64 id;
78 int nr_pages;
79 atomic_t pendcnt;
80 unsigned short operation;
81 int status;
82 struct list_head free_list;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +020083 DECLARE_BITMAP(unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040084};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040085
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040086#define BLKBACK_INVALID_HANDLE (~0)
87
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050088struct xen_blkbk {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040089 struct pending_req *pending_reqs;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040090 /* List of all 'pending_req' available */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050091 struct list_head pending_free;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040092 /* And its spinlock. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050093 spinlock_t pending_free_lock;
94 wait_queue_head_t pending_free_wq;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040095 /* The list of all pages that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050096 struct page **pending_pages;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040097 /* And the grant handles that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050098 grant_handle_t *pending_grant_handles;
99};
100
101static struct xen_blkbk *blkbk;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400102
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400103/*
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200104 * Maximum number of grant pages that can be mapped in blkback.
105 * BLKIF_MAX_SEGMENTS_PER_REQUEST * RING_SIZE is the maximum number of
106 * pages that blkback will persistently map.
107 * Currently, this is:
108 * RING_SIZE = 32 (for all known ring types)
109 * BLKIF_MAX_SEGMENTS_PER_REQUEST = 11
110 * sizeof(struct persistent_gnt) = 48
111 * So the maximum memory used to store the grants is:
112 * 32 * 11 * 48 = 16896 bytes
113 */
114static inline unsigned int max_mapped_grant_pages(enum blkif_protocol protocol)
115{
116 switch (protocol) {
117 case BLKIF_PROTOCOL_NATIVE:
118 return __CONST_RING_SIZE(blkif, PAGE_SIZE) *
119 BLKIF_MAX_SEGMENTS_PER_REQUEST;
120 case BLKIF_PROTOCOL_X86_32:
121 return __CONST_RING_SIZE(blkif_x86_32, PAGE_SIZE) *
122 BLKIF_MAX_SEGMENTS_PER_REQUEST;
123 case BLKIF_PROTOCOL_X86_64:
124 return __CONST_RING_SIZE(blkif_x86_64, PAGE_SIZE) *
125 BLKIF_MAX_SEGMENTS_PER_REQUEST;
126 default:
127 BUG();
128 }
129 return 0;
130}
131
132
133/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400134 * Little helpful macro to figure out the index and virtual address of the
135 * pending_pages[..]. For each 'pending_req' we have have up to
136 * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400137 * 10 and would index in the pending_pages[..].
138 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400139static inline int vaddr_pagenr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400140{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400141 return (req - blkbk->pending_reqs) *
142 BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400143}
144
Jan Beulichefe08a32010-02-05 14:19:33 -0500145#define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
146
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400147static inline unsigned long vaddr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400148{
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500149 unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400150 return (unsigned long)pfn_to_kaddr(pfn);
151}
152
153#define pending_handle(_req, _seg) \
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500154 (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400155
156
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400157static int do_block_io_op(struct xen_blkif *blkif);
158static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400159 struct blkif_request *req,
160 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400161static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400162 unsigned short op, int st);
163
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100164#define foreach_grant_safe(pos, n, rbtree, node) \
165 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
166 (n) = rb_next(&(pos)->node); \
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200167 &(pos)->node != NULL; \
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100168 (pos) = container_of(n, typeof(*(pos)), node), \
169 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200170
171
172static void add_persistent_gnt(struct rb_root *root,
173 struct persistent_gnt *persistent_gnt)
174{
175 struct rb_node **new = &(root->rb_node), *parent = NULL;
176 struct persistent_gnt *this;
177
178 /* Figure out where to put new node */
179 while (*new) {
180 this = container_of(*new, struct persistent_gnt, node);
181
182 parent = *new;
183 if (persistent_gnt->gnt < this->gnt)
184 new = &((*new)->rb_left);
185 else if (persistent_gnt->gnt > this->gnt)
186 new = &((*new)->rb_right);
187 else {
188 pr_alert(DRV_PFX " trying to add a gref that's already in the tree\n");
189 BUG();
190 }
191 }
192
193 /* Add new node and rebalance tree. */
194 rb_link_node(&(persistent_gnt->node), parent, new);
195 rb_insert_color(&(persistent_gnt->node), root);
196}
197
198static struct persistent_gnt *get_persistent_gnt(struct rb_root *root,
199 grant_ref_t gref)
200{
201 struct persistent_gnt *data;
202 struct rb_node *node = root->rb_node;
203
204 while (node) {
205 data = container_of(node, struct persistent_gnt, node);
206
207 if (gref < data->gnt)
208 node = node->rb_left;
209 else if (gref > data->gnt)
210 node = node->rb_right;
211 else
212 return data;
213 }
214 return NULL;
215}
216
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100217static void free_persistent_gnts(struct rb_root *root, unsigned int num)
218{
219 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
220 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
221 struct persistent_gnt *persistent_gnt;
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100222 struct rb_node *n;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100223 int ret = 0;
224 int segs_to_unmap = 0;
225
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100226 foreach_grant_safe(persistent_gnt, n, root, node) {
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100227 BUG_ON(persistent_gnt->handle ==
228 BLKBACK_INVALID_HANDLE);
229 gnttab_set_unmap_op(&unmap[segs_to_unmap],
230 (unsigned long) pfn_to_kaddr(page_to_pfn(
231 persistent_gnt->page)),
232 GNTMAP_host_map,
233 persistent_gnt->handle);
234
235 pages[segs_to_unmap] = persistent_gnt->page;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100236
237 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
238 !rb_next(&persistent_gnt->node)) {
239 ret = gnttab_unmap_refs(unmap, NULL, pages,
240 segs_to_unmap);
241 BUG_ON(ret);
242 segs_to_unmap = 0;
243 }
Roger Pau Monne7dc34112012-12-04 15:21:52 +0100244
245 rb_erase(&persistent_gnt->node, root);
246 kfree(persistent_gnt);
247 num--;
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100248 }
249 BUG_ON(num != 0);
250}
251
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400252/*
253 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400254 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400255static struct pending_req *alloc_req(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400256{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400257 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400258 unsigned long flags;
259
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500260 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
261 if (!list_empty(&blkbk->pending_free)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400262 req = list_entry(blkbk->pending_free.next, struct pending_req,
263 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400264 list_del(&req->free_list);
265 }
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500266 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400267 return req;
268}
269
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400270/*
271 * Return the 'pending_req' structure back to the freepool. We also
272 * wake up the thread if it was waiting for a free page.
273 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400274static void free_req(struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400275{
276 unsigned long flags;
277 int was_empty;
278
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500279 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
280 was_empty = list_empty(&blkbk->pending_free);
281 list_add(&req->free_list, &blkbk->pending_free);
282 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400283 if (was_empty)
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500284 wake_up(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400285}
286
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400287/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400288 * Routines for managing virtual block devices (vbds).
289 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400290static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
291 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400292{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400293 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400294 int rc = -EACCES;
295
296 if ((operation != READ) && vbd->readonly)
297 goto out;
298
Jan Beulich8ab52152011-05-17 11:07:05 +0100299 if (likely(req->nr_sects)) {
300 blkif_sector_t end = req->sector_number + req->nr_sects;
301
302 if (unlikely(end < req->sector_number))
303 goto out;
304 if (unlikely(end > vbd_sz(vbd)))
305 goto out;
306 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400307
308 req->dev = vbd->pdevice;
309 req->bdev = vbd->bdev;
310 rc = 0;
311
312 out:
313 return rc;
314}
315
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400316static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400317{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400318 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400319 struct xenbus_transaction xbt;
320 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400321 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400322 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400323
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400324 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400325 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400326 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400327 vbd->size = new_size;
328again:
329 err = xenbus_transaction_start(&xbt);
330 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400331 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400332 return;
333 }
334 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400335 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400336 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400337 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400338 goto abort;
339 }
340 /*
341 * Write the current state; we will use this to synchronize
342 * the front-end. If the current state is "connected" the
343 * front-end will get the new size information online.
344 */
345 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
346 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400347 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400348 goto abort;
349 }
350
351 err = xenbus_transaction_end(xbt, 0);
352 if (err == -EAGAIN)
353 goto again;
354 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400355 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400356 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400357abort:
358 xenbus_transaction_end(xbt, 1);
359}
360
361/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400362 * Notification from the guest OS.
363 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400364static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400365{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400366 blkif->waiting_reqs = 1;
367 wake_up(&blkif->wq);
368}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400369
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400370irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400371{
372 blkif_notify_work(dev_id);
373 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400374}
375
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400376/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400377 * SCHEDULER FUNCTIONS
378 */
379
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400380static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400381{
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800382 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
383 " | ds %4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400384 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800385 blkif->st_rd_req, blkif->st_wr_req,
386 blkif->st_f_req, blkif->st_ds_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400387 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
388 blkif->st_rd_req = 0;
389 blkif->st_wr_req = 0;
390 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800391 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400392}
393
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400394int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400395{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400396 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400397 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400398
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400399 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400400
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400401 while (!kthread_should_stop()) {
402 if (try_to_freeze())
403 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400404 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400405 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400406
407 wait_event_interruptible(
408 blkif->wq,
409 blkif->waiting_reqs || kthread_should_stop());
410 wait_event_interruptible(
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500411 blkbk->pending_free_wq,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400412 !list_empty(&blkbk->pending_free) ||
413 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400414
415 blkif->waiting_reqs = 0;
416 smp_mb(); /* clear flag *before* checking for work */
417
418 if (do_block_io_op(blkif))
419 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400420
421 if (log_stats && time_after(jiffies, blkif->st_print))
422 print_stats(blkif);
423 }
424
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200425 /* Free all persistent grant pages */
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100426 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
427 free_persistent_gnts(&blkif->persistent_gnts,
428 blkif->persistent_gnt_c);
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200429
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200430 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100431 blkif->persistent_gnt_c = 0;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200432
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400433 if (log_stats)
434 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400435
436 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400437 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400438
439 return 0;
440}
441
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400442struct seg_buf {
443 unsigned long buf;
444 unsigned int nsec;
445};
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400446/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400447 * Unmap the grant references, and also remove the M2P over-rides
448 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400449 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400450static void xen_blkbk_unmap(struct pending_req *req)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400451{
452 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500453 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400454 unsigned int i, invcount = 0;
455 grant_handle_t handle;
456 int ret;
457
458 for (i = 0; i < req->nr_pages; i++) {
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200459 if (!test_bit(i, req->unmap_seg))
460 continue;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400461 handle = pending_handle(req, i);
462 if (handle == BLKBACK_INVALID_HANDLE)
463 continue;
464 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
465 GNTMAP_host_map, handle);
466 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500467 pages[invcount] = virt_to_page(vaddr(req, i));
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400468 invcount++;
469 }
470
Stefano Stabellini2fc136e2012-09-12 12:44:30 +0100471 ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400472 BUG_ON(ret);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400473}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400474
475static int xen_blkbk_map(struct blkif_request *req,
476 struct pending_req *pending_req,
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200477 struct seg_buf seg[],
478 struct page *pages[])
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400479{
480 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200481 struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
482 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
483 struct persistent_gnt *persistent_gnt = NULL;
484 struct xen_blkif *blkif = pending_req->blkif;
485 phys_addr_t addr = 0;
486 int i, j;
487 bool new_map;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400488 int nseg = req->u.rw.nr_segments;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200489 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400490 int ret = 0;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200491 int use_persistent_gnts;
492
493 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
494
495 BUG_ON(blkif->persistent_gnt_c >
496 max_mapped_grant_pages(pending_req->blkif->blk_protocol));
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400497
498 /*
499 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400500 * assign map[..] with the PFN of the page in our domain with the
501 * corresponding grant reference for each page.
502 */
503 for (i = 0; i < nseg; i++) {
504 uint32_t flags;
505
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200506 if (use_persistent_gnts)
507 persistent_gnt = get_persistent_gnt(
508 &blkif->persistent_gnts,
509 req->u.rw.seg[i].gref);
510
511 if (persistent_gnt) {
512 /*
513 * We are using persistent grants and
514 * the grant is already mapped
515 */
516 new_map = false;
517 } else if (use_persistent_gnts &&
518 blkif->persistent_gnt_c <
519 max_mapped_grant_pages(blkif->blk_protocol)) {
520 /*
521 * We are using persistent grants, the grant is
522 * not mapped but we have room for it
523 */
524 new_map = true;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100525 persistent_gnt = kmalloc(
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200526 sizeof(struct persistent_gnt),
527 GFP_KERNEL);
528 if (!persistent_gnt)
529 return -ENOMEM;
530 persistent_gnt->page = alloc_page(GFP_KERNEL);
531 if (!persistent_gnt->page) {
532 kfree(persistent_gnt);
533 return -ENOMEM;
534 }
535 persistent_gnt->gnt = req->u.rw.seg[i].gref;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100536 persistent_gnt->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200537
538 pages_to_gnt[segs_to_map] =
539 persistent_gnt->page;
540 addr = (unsigned long) pfn_to_kaddr(
541 page_to_pfn(persistent_gnt->page));
542
543 add_persistent_gnt(&blkif->persistent_gnts,
544 persistent_gnt);
545 blkif->persistent_gnt_c++;
546 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
547 persistent_gnt->gnt, blkif->persistent_gnt_c,
548 max_mapped_grant_pages(blkif->blk_protocol));
549 } else {
550 /*
551 * We are either using persistent grants and
552 * hit the maximum limit of grants mapped,
553 * or we are not using persistent grants.
554 */
555 if (use_persistent_gnts &&
556 !blkif->vbd.overflow_max_grants) {
557 blkif->vbd.overflow_max_grants = 1;
558 pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
559 blkif->domid, blkif->vbd.handle);
560 }
561 new_map = true;
562 pages[i] = blkbk->pending_page(pending_req, i);
563 addr = vaddr(pending_req, i);
564 pages_to_gnt[segs_to_map] =
565 blkbk->pending_page(pending_req, i);
566 }
567
568 if (persistent_gnt) {
569 pages[i] = persistent_gnt->page;
570 persistent_gnts[i] = persistent_gnt;
571 } else {
572 persistent_gnts[i] = NULL;
573 }
574
575 if (new_map) {
576 flags = GNTMAP_host_map;
577 if (!persistent_gnt &&
578 (pending_req->operation != BLKIF_OP_READ))
579 flags |= GNTMAP_readonly;
580 gnttab_set_map_op(&map[segs_to_map++], addr,
581 flags, req->u.rw.seg[i].gref,
582 blkif->domid);
583 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400584 }
585
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200586 if (segs_to_map) {
587 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
588 BUG_ON(ret);
589 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400590
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400591 /*
592 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400593 * so that when we access vaddr(pending_req,i) it has the contents of
594 * the page from the other domain.
595 */
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200596 bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
597 for (i = 0, j = 0; i < nseg; i++) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100598 if (!persistent_gnts[i] ||
599 persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200600 /* This is a newly mapped grant */
601 BUG_ON(j >= segs_to_map);
602 if (unlikely(map[j].status != 0)) {
603 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
604 map[j].handle = BLKBACK_INVALID_HANDLE;
605 ret |= 1;
606 if (persistent_gnts[i]) {
607 rb_erase(&persistent_gnts[i]->node,
608 &blkif->persistent_gnts);
609 blkif->persistent_gnt_c--;
610 kfree(persistent_gnts[i]);
611 persistent_gnts[i] = NULL;
612 }
613 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400614 }
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200615 if (persistent_gnts[i]) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100616 if (persistent_gnts[i]->handle ==
617 BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200618 /*
619 * If this is a new persistent grant
620 * save the handler
621 */
622 persistent_gnts[i]->handle = map[j].handle;
623 persistent_gnts[i]->dev_bus_addr =
624 map[j++].dev_bus_addr;
625 }
626 pending_handle(pending_req, i) =
627 persistent_gnts[i]->handle;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400628
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200629 if (ret)
630 continue;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400631
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200632 seg[i].buf = persistent_gnts[i]->dev_bus_addr |
633 (req->u.rw.seg[i].first_sect << 9);
634 } else {
635 pending_handle(pending_req, i) = map[j].handle;
636 bitmap_set(pending_req->unmap_seg, i, 1);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400637
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200638 if (ret) {
639 j++;
640 continue;
641 }
642
643 seg[i].buf = map[j++].dev_bus_addr |
644 (req->u.rw.seg[i].first_sect << 9);
645 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400646 }
647 return ret;
648}
649
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400650static int dispatch_discard_io(struct xen_blkif *blkif,
651 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800652{
653 int err = 0;
654 int status = BLKIF_RSP_OKAY;
655 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400656 unsigned long secure;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800657
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400658 blkif->st_ds_req++;
659
660 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400661 secure = (blkif->vbd.discard_secure &&
662 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
663 BLKDEV_DISCARD_SECURE : 0;
664
665 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
666 req->u.discard.nr_sectors,
667 GFP_KERNEL, secure);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800668
669 if (err == -EOPNOTSUPP) {
670 pr_debug(DRV_PFX "discard op failed, not supported\n");
671 status = BLKIF_RSP_EOPNOTSUPP;
672 } else if (err)
673 status = BLKIF_RSP_ERROR;
674
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400675 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400676 xen_blkif_put(blkif);
677 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800678}
679
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400680static void xen_blk_drain_io(struct xen_blkif *blkif)
681{
682 atomic_set(&blkif->drain, 1);
683 do {
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -0400684 /* The initial value is one, and one refcnt taken at the
685 * start of the xen_blkif_schedule thread. */
686 if (atomic_read(&blkif->refcnt) <= 2)
687 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400688 wait_for_completion_interruptible_timeout(
689 &blkif->drain_complete, HZ);
690
691 if (!atomic_read(&blkif->drain))
692 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400693 } while (!kthread_should_stop());
694 atomic_set(&blkif->drain, 0);
695}
696
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400697/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400698 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400699 */
700
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400701static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400702{
703 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400704 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400705 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400706 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400707 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400708 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400709 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
710 (error == -EOPNOTSUPP)) {
711 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
712 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
713 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400714 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400715 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400716 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400717 pending_req->status = BLKIF_RSP_ERROR;
718 }
719
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400720 /*
721 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400722 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400723 * the proper response on the ring.
724 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400725 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400726 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400727 make_response(pending_req->blkif, pending_req->id,
728 pending_req->operation, pending_req->status);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400729 xen_blkif_put(pending_req->blkif);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400730 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
731 if (atomic_read(&pending_req->blkif->drain))
732 complete(&pending_req->blkif->drain_complete);
733 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400734 free_req(pending_req);
735 }
736}
737
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400738/*
739 * bio callback.
740 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800741static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400742{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400743 __end_block_io_op(bio->bi_private, error);
744 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400745}
746
747
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400749/*
750 * Function to copy the from the ring buffer the 'struct blkif_request'
751 * (which has the sectors we want, number of them, grant references, etc),
752 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400753 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700754static int
755__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400756{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800757 union blkif_back_rings *blk_rings = &blkif->blk_rings;
758 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400759 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400760 RING_IDX rc, rp;
761 int more_to_do = 0;
762
763 rc = blk_rings->common.req_cons;
764 rp = blk_rings->common.sring->req_prod;
765 rmb(); /* Ensure we see queued requests up to 'rp'. */
766
767 while (rc != rp) {
768
769 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
770 break;
771
Keir Fraser8270b452009-03-06 08:29:15 +0000772 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400773 more_to_do = 1;
774 break;
775 }
776
Keir Fraser8270b452009-03-06 08:29:15 +0000777 pending_req = alloc_req();
778 if (NULL == pending_req) {
779 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400780 more_to_do = 1;
781 break;
782 }
783
784 switch (blkif->blk_protocol) {
785 case BLKIF_PROTOCOL_NATIVE:
786 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
787 break;
788 case BLKIF_PROTOCOL_X86_32:
789 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
790 break;
791 case BLKIF_PROTOCOL_X86_64:
792 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
793 break;
794 default:
795 BUG();
796 }
797 blk_rings->common.req_cons = ++rc; /* before make_response() */
798
799 /* Apply all sanity checks to /private copy/ of request. */
800 barrier();
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400801 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
802 free_req(pending_req);
803 if (dispatch_discard_io(blkif, &req))
804 break;
805 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400806 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400807
808 /* Yield point for this unbounded loop. */
809 cond_resched();
810 }
811
812 return more_to_do;
813}
814
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700815static int
816do_block_io_op(struct xen_blkif *blkif)
817{
818 union blkif_back_rings *blk_rings = &blkif->blk_rings;
819 int more_to_do;
820
821 do {
822 more_to_do = __do_block_io_op(blkif);
823 if (more_to_do)
824 break;
825
826 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
827 } while (more_to_do);
828
829 return more_to_do;
830}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400831/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400832 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
833 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400834 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400835static int dispatch_rw_block_io(struct xen_blkif *blkif,
836 struct blkif_request *req,
837 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400838{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400839 struct phys_req preq;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400840 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400841 unsigned int nseg;
842 struct bio *bio = NULL;
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400843 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400844 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400845 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400846 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400847 bool drain = false;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200848 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400849
850 switch (req->operation) {
851 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400852 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400853 operation = READ;
854 break;
855 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400856 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -0400857 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400858 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400859 case BLKIF_OP_WRITE_BARRIER:
860 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400861 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400862 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400863 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400864 break;
865 default:
866 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400867 goto fail_response;
868 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400869 }
870
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400871 /* Check that the number of segments is sane. */
872 nseg = req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400873
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400874 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400875 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400876 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400877 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400878 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400879 goto fail_response;
880 }
881
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500882 preq.sector_number = req->u.rw.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400883 preq.nr_sects = 0;
884
885 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400886 pending_req->id = req->u.rw.id;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400887 pending_req->operation = req->operation;
888 pending_req->status = BLKIF_RSP_OKAY;
889 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400890
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400891 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500892 seg[i].nsec = req->u.rw.seg[i].last_sect -
893 req->u.rw.seg[i].first_sect + 1;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500894 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
895 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400896 goto fail_response;
897 preq.nr_sects += seg[i].nsec;
Konrad Rzeszutek Wilk976222e2011-04-15 11:38:29 -0400898
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400899 }
900
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400901 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400902 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400903 operation == READ ? "read" : "write",
904 preq.sector_number,
905 preq.sector_number + preq.nr_sects, preq.dev);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400906 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400907 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400908
909 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400910 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400911 * is set there.
912 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400913 for (i = 0; i < nseg; i++) {
914 if (((int)preq.sector_number|(int)seg[i].nsec) &
915 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400916 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400917 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400918 goto fail_response;
919 }
920 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400921
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400922 /* Wait on all outstanding I/O's and once that has been completed
923 * issue the WRITE_FLUSH.
924 */
925 if (drain)
926 xen_blk_drain_io(pending_req->blkif);
927
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400928 /*
929 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400930 * set gnttab_set_unmap_op on all of the grant references and perform
931 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400932 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400933 */
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200934 if (xen_blkbk_map(req, pending_req, seg, pages))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400935 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400936
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800937 /*
938 * This corresponding xen_blkif_put is done in __end_block_io_op, or
939 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
940 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400941 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400942
943 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400944 while ((bio == NULL) ||
945 (bio_add_page(bio,
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200946 pages[i],
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400947 seg[i].nsec << 9,
948 seg[i].buf & ~PAGE_MASK) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400949
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400950 bio = bio_alloc(GFP_KERNEL, nseg-i);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400951 if (unlikely(bio == NULL))
952 goto fail_put_bio;
953
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400954 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400955 bio->bi_bdev = preq.bdev;
956 bio->bi_private = pending_req;
957 bio->bi_end_io = end_block_io_op;
958 bio->bi_sector = preq.sector_number;
959 }
960
961 preq.sector_number += seg[i].nsec;
962 }
963
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800964 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400965 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400966 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -0400967
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400968 bio = bio_alloc(GFP_KERNEL, 0);
969 if (unlikely(bio == NULL))
970 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400971
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400972 biolist[nbio++] = bio;
973 bio->bi_bdev = preq.bdev;
974 bio->bi_private = pending_req;
975 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400976 }
977
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400978 /*
979 * We set it one so that the last submit_bio does not have to call
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400980 * atomic_inc.
981 */
982 atomic_set(&pending_req->pendcnt, nbio);
983
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400984 /* Get a reference count for the disk queue and start sending I/O */
985 blk_start_plug(&plug);
986
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400987 for (i = 0; i < nbio; i++)
988 submit_bio(operation, biolist[i]);
989
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400990 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400991 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400992
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400993 if (operation == READ)
994 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -0400995 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400996 blkif->st_wr_sect += preq.nr_sects;
997
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400998 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400999
1000 fail_flush:
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -04001001 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001002 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001003 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001004 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001005 free_req(pending_req);
1006 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001007 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001008
1009 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001010 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001011 bio_put(biolist[i]);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001012 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001013 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001014 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001015}
1016
1017
1018
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001019/*
1020 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001021 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001022static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001023 unsigned short op, int st)
1024{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001025 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001026 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001027 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001028 int notify;
1029
1030 resp.id = id;
1031 resp.operation = op;
1032 resp.status = st;
1033
1034 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1035 /* Place on the response ring for the relevant domain. */
1036 switch (blkif->blk_protocol) {
1037 case BLKIF_PROTOCOL_NATIVE:
1038 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1039 &resp, sizeof(resp));
1040 break;
1041 case BLKIF_PROTOCOL_X86_32:
1042 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1043 &resp, sizeof(resp));
1044 break;
1045 case BLKIF_PROTOCOL_X86_64:
1046 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1047 &resp, sizeof(resp));
1048 break;
1049 default:
1050 BUG();
1051 }
1052 blk_rings->common.rsp_prod_pvt++;
1053 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001054 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001055 if (notify)
1056 notify_remote_via_irq(blkif->irq);
1057}
1058
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001059static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001060{
1061 int i, mmap_pages;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001062 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001063
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001064 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001065 return -ENODEV;
1066
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001067 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001068 if (!blkbk) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001069 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001070 return -ENOMEM;
1071 }
1072
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001073 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001074
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001075 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001076 xen_blkif_reqs, GFP_KERNEL);
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001077 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001078 mmap_pages, GFP_KERNEL);
1079 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
1080 mmap_pages, GFP_KERNEL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001081
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001082 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
1083 !blkbk->pending_pages) {
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001084 rc = -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001085 goto out_of_memory;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001086 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001087
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001088 for (i = 0; i < mmap_pages; i++) {
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001089 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001090 blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001091 if (blkbk->pending_pages[i] == NULL) {
1092 rc = -ENOMEM;
1093 goto out_of_memory;
1094 }
1095 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001096 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001097 if (rc)
1098 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001099
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001100 INIT_LIST_HEAD(&blkbk->pending_free);
1101 spin_lock_init(&blkbk->pending_free_lock);
1102 init_waitqueue_head(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001103
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001104 for (i = 0; i < xen_blkif_reqs; i++)
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001105 list_add_tail(&blkbk->pending_reqs[i].free_list,
1106 &blkbk->pending_free);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001107
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001108 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001109 if (rc)
1110 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001111
1112 return 0;
1113
1114 out_of_memory:
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001115 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001116 failed_init:
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001117 kfree(blkbk->pending_reqs);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001118 kfree(blkbk->pending_grant_handles);
Dan Carpenter9b83c772011-05-27 09:27:16 +03001119 if (blkbk->pending_pages) {
1120 for (i = 0; i < mmap_pages; i++) {
1121 if (blkbk->pending_pages[i])
1122 __free_page(blkbk->pending_pages[i]);
1123 }
1124 kfree(blkbk->pending_pages);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001125 }
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001126 kfree(blkbk);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001127 blkbk = NULL;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001128 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001129}
1130
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001131module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001132
1133MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001134MODULE_ALIAS("xen-backend:vbd");