blob: 74374fb762aa5bcf835e73a7390f12b7403525ee [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>
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 Monne0a8704a2012-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 Monne0a8704a2012-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 Monne0a8704a2012-10-24 18:58:45 +0200164#define foreach_grant(pos, rbtree, node) \
165 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node); \
166 &(pos)->node != NULL; \
167 (pos) = container_of(rb_next(&(pos)->node), typeof(*(pos)), node))
168
169
170static void add_persistent_gnt(struct rb_root *root,
171 struct persistent_gnt *persistent_gnt)
172{
173 struct rb_node **new = &(root->rb_node), *parent = NULL;
174 struct persistent_gnt *this;
175
176 /* Figure out where to put new node */
177 while (*new) {
178 this = container_of(*new, struct persistent_gnt, node);
179
180 parent = *new;
181 if (persistent_gnt->gnt < this->gnt)
182 new = &((*new)->rb_left);
183 else if (persistent_gnt->gnt > this->gnt)
184 new = &((*new)->rb_right);
185 else {
186 pr_alert(DRV_PFX " trying to add a gref that's already in the tree\n");
187 BUG();
188 }
189 }
190
191 /* Add new node and rebalance tree. */
192 rb_link_node(&(persistent_gnt->node), parent, new);
193 rb_insert_color(&(persistent_gnt->node), root);
194}
195
196static struct persistent_gnt *get_persistent_gnt(struct rb_root *root,
197 grant_ref_t gref)
198{
199 struct persistent_gnt *data;
200 struct rb_node *node = root->rb_node;
201
202 while (node) {
203 data = container_of(node, struct persistent_gnt, node);
204
205 if (gref < data->gnt)
206 node = node->rb_left;
207 else if (gref > data->gnt)
208 node = node->rb_right;
209 else
210 return data;
211 }
212 return NULL;
213}
214
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100215static void free_persistent_gnts(struct rb_root *root, unsigned int num)
216{
217 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
218 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
219 struct persistent_gnt *persistent_gnt;
220 int ret = 0;
221 int segs_to_unmap = 0;
222
223 foreach_grant(persistent_gnt, root, node) {
224 BUG_ON(persistent_gnt->handle ==
225 BLKBACK_INVALID_HANDLE);
226 gnttab_set_unmap_op(&unmap[segs_to_unmap],
227 (unsigned long) pfn_to_kaddr(page_to_pfn(
228 persistent_gnt->page)),
229 GNTMAP_host_map,
230 persistent_gnt->handle);
231
232 pages[segs_to_unmap] = persistent_gnt->page;
233 rb_erase(&persistent_gnt->node, root);
234 kfree(persistent_gnt);
235 num--;
236
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 }
244 }
245 BUG_ON(num != 0);
246}
247
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400248/*
249 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400250 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400251static struct pending_req *alloc_req(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400252{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400253 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400254 unsigned long flags;
255
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500256 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
257 if (!list_empty(&blkbk->pending_free)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400258 req = list_entry(blkbk->pending_free.next, struct pending_req,
259 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400260 list_del(&req->free_list);
261 }
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500262 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400263 return req;
264}
265
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400266/*
267 * Return the 'pending_req' structure back to the freepool. We also
268 * wake up the thread if it was waiting for a free page.
269 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400270static void free_req(struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400271{
272 unsigned long flags;
273 int was_empty;
274
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500275 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
276 was_empty = list_empty(&blkbk->pending_free);
277 list_add(&req->free_list, &blkbk->pending_free);
278 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400279 if (was_empty)
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500280 wake_up(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400281}
282
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400283/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400284 * Routines for managing virtual block devices (vbds).
285 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400286static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
287 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400288{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400289 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400290 int rc = -EACCES;
291
292 if ((operation != READ) && vbd->readonly)
293 goto out;
294
Jan Beulich8ab52152011-05-17 11:07:05 +0100295 if (likely(req->nr_sects)) {
296 blkif_sector_t end = req->sector_number + req->nr_sects;
297
298 if (unlikely(end < req->sector_number))
299 goto out;
300 if (unlikely(end > vbd_sz(vbd)))
301 goto out;
302 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400303
304 req->dev = vbd->pdevice;
305 req->bdev = vbd->bdev;
306 rc = 0;
307
308 out:
309 return rc;
310}
311
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400312static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400313{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400314 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400315 struct xenbus_transaction xbt;
316 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400317 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400318 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400319
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400320 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400321 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400322 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400323 vbd->size = new_size;
324again:
325 err = xenbus_transaction_start(&xbt);
326 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400327 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400328 return;
329 }
330 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400331 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400332 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400333 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400334 goto abort;
335 }
336 /*
337 * Write the current state; we will use this to synchronize
338 * the front-end. If the current state is "connected" the
339 * front-end will get the new size information online.
340 */
341 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
342 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400343 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400344 goto abort;
345 }
346
347 err = xenbus_transaction_end(xbt, 0);
348 if (err == -EAGAIN)
349 goto again;
350 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400351 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400352 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400353abort:
354 xenbus_transaction_end(xbt, 1);
355}
356
357/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400358 * Notification from the guest OS.
359 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400360static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400361{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400362 blkif->waiting_reqs = 1;
363 wake_up(&blkif->wq);
364}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400365
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400366irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400367{
368 blkif_notify_work(dev_id);
369 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400370}
371
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400372/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400373 * SCHEDULER FUNCTIONS
374 */
375
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400376static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400377{
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800378 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
379 " | ds %4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400380 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800381 blkif->st_rd_req, blkif->st_wr_req,
382 blkif->st_f_req, blkif->st_ds_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400383 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
384 blkif->st_rd_req = 0;
385 blkif->st_wr_req = 0;
386 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800387 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400388}
389
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400390int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400391{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400392 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400393 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400394
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400395 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400396
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400397 while (!kthread_should_stop()) {
398 if (try_to_freeze())
399 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400400 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400401 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400402
403 wait_event_interruptible(
404 blkif->wq,
405 blkif->waiting_reqs || kthread_should_stop());
406 wait_event_interruptible(
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500407 blkbk->pending_free_wq,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400408 !list_empty(&blkbk->pending_free) ||
409 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400410
411 blkif->waiting_reqs = 0;
412 smp_mb(); /* clear flag *before* checking for work */
413
414 if (do_block_io_op(blkif))
415 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400416
417 if (log_stats && time_after(jiffies, blkif->st_print))
418 print_stats(blkif);
419 }
420
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200421 /* Free all persistent grant pages */
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100422 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
423 free_persistent_gnts(&blkif->persistent_gnts,
424 blkif->persistent_gnt_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200425
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200426 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
Roger Pau Monne4d4f2702012-11-16 19:26:48 +0100427 blkif->persistent_gnt_c = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200428
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400429 if (log_stats)
430 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400431
432 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400433 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400434
435 return 0;
436}
437
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400438struct seg_buf {
439 unsigned long buf;
440 unsigned int nsec;
441};
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400442/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400443 * Unmap the grant references, and also remove the M2P over-rides
444 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400445 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400446static void xen_blkbk_unmap(struct pending_req *req)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400447{
448 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500449 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400450 unsigned int i, invcount = 0;
451 grant_handle_t handle;
452 int ret;
453
454 for (i = 0; i < req->nr_pages; i++) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200455 if (!test_bit(i, req->unmap_seg))
456 continue;
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400457 handle = pending_handle(req, i);
458 if (handle == BLKBACK_INVALID_HANDLE)
459 continue;
460 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
461 GNTMAP_host_map, handle);
462 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500463 pages[invcount] = virt_to_page(vaddr(req, i));
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400464 invcount++;
465 }
466
Stefano Stabellini2fc136e2012-09-12 12:44:30 +0100467 ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400468 BUG_ON(ret);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400469}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400470
471static int xen_blkbk_map(struct blkif_request *req,
472 struct pending_req *pending_req,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200473 struct seg_buf seg[],
474 struct page *pages[])
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400475{
476 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200477 struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
478 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
479 struct persistent_gnt *persistent_gnt = NULL;
480 struct xen_blkif *blkif = pending_req->blkif;
481 phys_addr_t addr = 0;
482 int i, j;
483 bool new_map;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400484 int nseg = req->u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200485 int segs_to_map = 0;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400486 int ret = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200487 int use_persistent_gnts;
488
489 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
490
491 BUG_ON(blkif->persistent_gnt_c >
492 max_mapped_grant_pages(pending_req->blkif->blk_protocol));
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400493
494 /*
495 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400496 * assign map[..] with the PFN of the page in our domain with the
497 * corresponding grant reference for each page.
498 */
499 for (i = 0; i < nseg; i++) {
500 uint32_t flags;
501
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200502 if (use_persistent_gnts)
503 persistent_gnt = get_persistent_gnt(
504 &blkif->persistent_gnts,
505 req->u.rw.seg[i].gref);
506
507 if (persistent_gnt) {
508 /*
509 * We are using persistent grants and
510 * the grant is already mapped
511 */
512 new_map = false;
513 } else if (use_persistent_gnts &&
514 blkif->persistent_gnt_c <
515 max_mapped_grant_pages(blkif->blk_protocol)) {
516 /*
517 * We are using persistent grants, the grant is
518 * not mapped but we have room for it
519 */
520 new_map = true;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100521 persistent_gnt = kmalloc(
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200522 sizeof(struct persistent_gnt),
523 GFP_KERNEL);
524 if (!persistent_gnt)
525 return -ENOMEM;
526 persistent_gnt->page = alloc_page(GFP_KERNEL);
527 if (!persistent_gnt->page) {
528 kfree(persistent_gnt);
529 return -ENOMEM;
530 }
531 persistent_gnt->gnt = req->u.rw.seg[i].gref;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100532 persistent_gnt->handle = BLKBACK_INVALID_HANDLE;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200533
534 pages_to_gnt[segs_to_map] =
535 persistent_gnt->page;
536 addr = (unsigned long) pfn_to_kaddr(
537 page_to_pfn(persistent_gnt->page));
538
539 add_persistent_gnt(&blkif->persistent_gnts,
540 persistent_gnt);
541 blkif->persistent_gnt_c++;
542 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
543 persistent_gnt->gnt, blkif->persistent_gnt_c,
544 max_mapped_grant_pages(blkif->blk_protocol));
545 } else {
546 /*
547 * We are either using persistent grants and
548 * hit the maximum limit of grants mapped,
549 * or we are not using persistent grants.
550 */
551 if (use_persistent_gnts &&
552 !blkif->vbd.overflow_max_grants) {
553 blkif->vbd.overflow_max_grants = 1;
554 pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
555 blkif->domid, blkif->vbd.handle);
556 }
557 new_map = true;
558 pages[i] = blkbk->pending_page(pending_req, i);
559 addr = vaddr(pending_req, i);
560 pages_to_gnt[segs_to_map] =
561 blkbk->pending_page(pending_req, i);
562 }
563
564 if (persistent_gnt) {
565 pages[i] = persistent_gnt->page;
566 persistent_gnts[i] = persistent_gnt;
567 } else {
568 persistent_gnts[i] = NULL;
569 }
570
571 if (new_map) {
572 flags = GNTMAP_host_map;
573 if (!persistent_gnt &&
574 (pending_req->operation != BLKIF_OP_READ))
575 flags |= GNTMAP_readonly;
576 gnttab_set_map_op(&map[segs_to_map++], addr,
577 flags, req->u.rw.seg[i].gref,
578 blkif->domid);
579 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400580 }
581
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200582 if (segs_to_map) {
583 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
584 BUG_ON(ret);
585 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400586
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400587 /*
588 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400589 * so that when we access vaddr(pending_req,i) it has the contents of
590 * the page from the other domain.
591 */
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200592 bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
593 for (i = 0, j = 0; i < nseg; i++) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100594 if (!persistent_gnts[i] ||
595 persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200596 /* This is a newly mapped grant */
597 BUG_ON(j >= segs_to_map);
598 if (unlikely(map[j].status != 0)) {
599 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
600 map[j].handle = BLKBACK_INVALID_HANDLE;
601 ret |= 1;
602 if (persistent_gnts[i]) {
603 rb_erase(&persistent_gnts[i]->node,
604 &blkif->persistent_gnts);
605 blkif->persistent_gnt_c--;
606 kfree(persistent_gnts[i]);
607 persistent_gnts[i] = NULL;
608 }
609 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400610 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200611 if (persistent_gnts[i]) {
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100612 if (persistent_gnts[i]->handle ==
613 BLKBACK_INVALID_HANDLE) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200614 /*
615 * If this is a new persistent grant
616 * save the handler
617 */
618 persistent_gnts[i]->handle = map[j].handle;
619 persistent_gnts[i]->dev_bus_addr =
620 map[j++].dev_bus_addr;
621 }
622 pending_handle(pending_req, i) =
623 persistent_gnts[i]->handle;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400624
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200625 if (ret)
626 continue;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400627
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200628 seg[i].buf = persistent_gnts[i]->dev_bus_addr |
629 (req->u.rw.seg[i].first_sect << 9);
630 } else {
631 pending_handle(pending_req, i) = map[j].handle;
632 bitmap_set(pending_req->unmap_seg, i, 1);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400633
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200634 if (ret) {
635 j++;
636 continue;
637 }
638
639 seg[i].buf = map[j++].dev_bus_addr |
640 (req->u.rw.seg[i].first_sect << 9);
641 }
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400642 }
643 return ret;
644}
645
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400646static int dispatch_discard_io(struct xen_blkif *blkif,
647 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800648{
649 int err = 0;
650 int status = BLKIF_RSP_OKAY;
651 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400652 unsigned long secure;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800653
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400654 blkif->st_ds_req++;
655
656 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400657 secure = (blkif->vbd.discard_secure &&
658 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
659 BLKDEV_DISCARD_SECURE : 0;
660
661 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
662 req->u.discard.nr_sectors,
663 GFP_KERNEL, secure);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800664
665 if (err == -EOPNOTSUPP) {
666 pr_debug(DRV_PFX "discard op failed, not supported\n");
667 status = BLKIF_RSP_EOPNOTSUPP;
668 } else if (err)
669 status = BLKIF_RSP_ERROR;
670
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400671 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400672 xen_blkif_put(blkif);
673 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800674}
675
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400676static void xen_blk_drain_io(struct xen_blkif *blkif)
677{
678 atomic_set(&blkif->drain, 1);
679 do {
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -0400680 /* The initial value is one, and one refcnt taken at the
681 * start of the xen_blkif_schedule thread. */
682 if (atomic_read(&blkif->refcnt) <= 2)
683 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400684 wait_for_completion_interruptible_timeout(
685 &blkif->drain_complete, HZ);
686
687 if (!atomic_read(&blkif->drain))
688 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400689 } while (!kthread_should_stop());
690 atomic_set(&blkif->drain, 0);
691}
692
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400693/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400694 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400695 */
696
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400697static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400698{
699 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400700 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400701 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400702 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400703 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400704 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400705 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
706 (error == -EOPNOTSUPP)) {
707 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
708 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
709 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400710 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400711 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400712 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400713 pending_req->status = BLKIF_RSP_ERROR;
714 }
715
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400716 /*
717 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400718 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400719 * the proper response on the ring.
720 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400721 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400722 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400723 make_response(pending_req->blkif, pending_req->id,
724 pending_req->operation, pending_req->status);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400725 xen_blkif_put(pending_req->blkif);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400726 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
727 if (atomic_read(&pending_req->blkif->drain))
728 complete(&pending_req->blkif->drain_complete);
729 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400730 free_req(pending_req);
731 }
732}
733
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400734/*
735 * bio callback.
736 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800737static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400738{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739 __end_block_io_op(bio->bi_private, error);
740 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400741}
742
743
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400744
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400745/*
746 * Function to copy the from the ring buffer the 'struct blkif_request'
747 * (which has the sectors we want, number of them, grant references, etc),
748 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400749 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700750static int
751__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400752{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800753 union blkif_back_rings *blk_rings = &blkif->blk_rings;
754 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400755 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400756 RING_IDX rc, rp;
757 int more_to_do = 0;
758
759 rc = blk_rings->common.req_cons;
760 rp = blk_rings->common.sring->req_prod;
761 rmb(); /* Ensure we see queued requests up to 'rp'. */
762
763 while (rc != rp) {
764
765 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
766 break;
767
Keir Fraser8270b452009-03-06 08:29:15 +0000768 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400769 more_to_do = 1;
770 break;
771 }
772
Keir Fraser8270b452009-03-06 08:29:15 +0000773 pending_req = alloc_req();
774 if (NULL == pending_req) {
775 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400776 more_to_do = 1;
777 break;
778 }
779
780 switch (blkif->blk_protocol) {
781 case BLKIF_PROTOCOL_NATIVE:
782 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
783 break;
784 case BLKIF_PROTOCOL_X86_32:
785 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
786 break;
787 case BLKIF_PROTOCOL_X86_64:
788 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
789 break;
790 default:
791 BUG();
792 }
793 blk_rings->common.req_cons = ++rc; /* before make_response() */
794
795 /* Apply all sanity checks to /private copy/ of request. */
796 barrier();
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400797 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
798 free_req(pending_req);
799 if (dispatch_discard_io(blkif, &req))
800 break;
801 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400802 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803
804 /* Yield point for this unbounded loop. */
805 cond_resched();
806 }
807
808 return more_to_do;
809}
810
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700811static int
812do_block_io_op(struct xen_blkif *blkif)
813{
814 union blkif_back_rings *blk_rings = &blkif->blk_rings;
815 int more_to_do;
816
817 do {
818 more_to_do = __do_block_io_op(blkif);
819 if (more_to_do)
820 break;
821
822 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
823 } while (more_to_do);
824
825 return more_to_do;
826}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400827/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400828 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
829 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400830 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400831static int dispatch_rw_block_io(struct xen_blkif *blkif,
832 struct blkif_request *req,
833 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400834{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400835 struct phys_req preq;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400836 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400837 unsigned int nseg;
838 struct bio *bio = NULL;
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400839 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400840 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400841 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400842 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400843 bool drain = false;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200844 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400845
846 switch (req->operation) {
847 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400848 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400849 operation = READ;
850 break;
851 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400852 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -0400853 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400854 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400855 case BLKIF_OP_WRITE_BARRIER:
856 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400857 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400858 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400859 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400860 break;
861 default:
862 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400863 goto fail_response;
864 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400865 }
866
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400867 /* Check that the number of segments is sane. */
868 nseg = req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400869
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400870 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400871 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400872 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400873 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400874 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400875 goto fail_response;
876 }
877
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400878 preq.dev = req->u.rw.handle;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500879 preq.sector_number = req->u.rw.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400880 preq.nr_sects = 0;
881
882 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400883 pending_req->id = req->u.rw.id;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400884 pending_req->operation = req->operation;
885 pending_req->status = BLKIF_RSP_OKAY;
886 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400887
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400888 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500889 seg[i].nsec = req->u.rw.seg[i].last_sect -
890 req->u.rw.seg[i].first_sect + 1;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500891 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
892 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400893 goto fail_response;
894 preq.nr_sects += seg[i].nsec;
Konrad Rzeszutek Wilk976222e2011-04-15 11:38:29 -0400895
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400896 }
897
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400898 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400899 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400900 operation == READ ? "read" : "write",
901 preq.sector_number,
902 preq.sector_number + preq.nr_sects, preq.dev);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400903 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400904 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400905
906 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400907 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400908 * is set there.
909 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400910 for (i = 0; i < nseg; i++) {
911 if (((int)preq.sector_number|(int)seg[i].nsec) &
912 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400913 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400914 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400915 goto fail_response;
916 }
917 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400918
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400919 /* Wait on all outstanding I/O's and once that has been completed
920 * issue the WRITE_FLUSH.
921 */
922 if (drain)
923 xen_blk_drain_io(pending_req->blkif);
924
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400925 /*
926 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400927 * set gnttab_set_unmap_op on all of the grant references and perform
928 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400929 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400930 */
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200931 if (xen_blkbk_map(req, pending_req, seg, pages))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400932 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400933
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800934 /*
935 * This corresponding xen_blkif_put is done in __end_block_io_op, or
936 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
937 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400938 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400939
940 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400941 while ((bio == NULL) ||
942 (bio_add_page(bio,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200943 pages[i],
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400944 seg[i].nsec << 9,
945 seg[i].buf & ~PAGE_MASK) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400946
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400947 bio = bio_alloc(GFP_KERNEL, nseg-i);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400948 if (unlikely(bio == NULL))
949 goto fail_put_bio;
950
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400951 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400952 bio->bi_bdev = preq.bdev;
953 bio->bi_private = pending_req;
954 bio->bi_end_io = end_block_io_op;
955 bio->bi_sector = preq.sector_number;
956 }
957
958 preq.sector_number += seg[i].nsec;
959 }
960
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800961 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400962 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400963 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -0400964
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400965 bio = bio_alloc(GFP_KERNEL, 0);
966 if (unlikely(bio == NULL))
967 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400968
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400969 biolist[nbio++] = bio;
970 bio->bi_bdev = preq.bdev;
971 bio->bi_private = pending_req;
972 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400973 }
974
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400975 /*
976 * We set it one so that the last submit_bio does not have to call
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400977 * atomic_inc.
978 */
979 atomic_set(&pending_req->pendcnt, nbio);
980
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400981 /* Get a reference count for the disk queue and start sending I/O */
982 blk_start_plug(&plug);
983
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400984 for (i = 0; i < nbio; i++)
985 submit_bio(operation, biolist[i]);
986
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400987 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400988 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400989
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400990 if (operation == READ)
991 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -0400992 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400993 blkif->st_wr_sect += preq.nr_sects;
994
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400995 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400996
997 fail_flush:
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400998 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400999 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -04001000 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001001 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001002 free_req(pending_req);
1003 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001004 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001005
1006 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -04001007 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -04001008 bio_put(biolist[i]);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001009 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001010 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -04001011 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001012}
1013
1014
1015
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04001016/*
1017 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001018 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -04001019static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001020 unsigned short op, int st)
1021{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001022 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001023 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -08001024 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001025 int notify;
1026
1027 resp.id = id;
1028 resp.operation = op;
1029 resp.status = st;
1030
1031 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1032 /* Place on the response ring for the relevant domain. */
1033 switch (blkif->blk_protocol) {
1034 case BLKIF_PROTOCOL_NATIVE:
1035 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1036 &resp, sizeof(resp));
1037 break;
1038 case BLKIF_PROTOCOL_X86_32:
1039 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1040 &resp, sizeof(resp));
1041 break;
1042 case BLKIF_PROTOCOL_X86_64:
1043 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1044 &resp, sizeof(resp));
1045 break;
1046 default:
1047 BUG();
1048 }
1049 blk_rings->common.rsp_prod_pvt++;
1050 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001051 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001052 if (notify)
1053 notify_remote_via_irq(blkif->irq);
1054}
1055
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001056static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001057{
1058 int i, mmap_pages;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001059 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001060
Daniel De Graafb2167ba2011-11-28 11:49:05 -05001061 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001062 return -ENODEV;
1063
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001064 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001065 if (!blkbk) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001066 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001067 return -ENOMEM;
1068 }
1069
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001070 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001071
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001072 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001073 xen_blkif_reqs, GFP_KERNEL);
Jan Beulich8e6dc6f2011-09-16 08:38:09 +01001074 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001075 mmap_pages, GFP_KERNEL);
1076 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
1077 mmap_pages, GFP_KERNEL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001078
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001079 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
1080 !blkbk->pending_pages) {
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001081 rc = -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001082 goto out_of_memory;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001083 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001084
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001085 for (i = 0; i < mmap_pages; i++) {
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001086 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001087 blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001088 if (blkbk->pending_pages[i] == NULL) {
1089 rc = -ENOMEM;
1090 goto out_of_memory;
1091 }
1092 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001093 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001094 if (rc)
1095 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001096
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001097 INIT_LIST_HEAD(&blkbk->pending_free);
1098 spin_lock_init(&blkbk->pending_free_lock);
1099 init_waitqueue_head(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001100
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001101 for (i = 0; i < xen_blkif_reqs; i++)
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -04001102 list_add_tail(&blkbk->pending_reqs[i].free_list,
1103 &blkbk->pending_free);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001104
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001105 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001106 if (rc)
1107 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001108
1109 return 0;
1110
1111 out_of_memory:
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -04001112 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001113 failed_init:
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001114 kfree(blkbk->pending_reqs);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001115 kfree(blkbk->pending_grant_handles);
Dan Carpenter9b83c772011-05-27 09:27:16 +03001116 if (blkbk->pending_pages) {
1117 for (i = 0; i < mmap_pages; i++) {
1118 if (blkbk->pending_pages[i])
1119 __free_page(blkbk->pending_pages[i]);
1120 }
1121 kfree(blkbk->pending_pages);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -05001122 }
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -04001123 kfree(blkbk);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -05001124 blkbk = NULL;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -04001125 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001126}
1127
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001128module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001129
1130MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +02001131MODULE_ALIAS("xen-backend:vbd");