blob: 280a13846e6cb06e1f774dd927598c1f3bfa5a8c [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>
Jeremy Fitzhardingeafd91d02009-09-15 14:12:37 -070042
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080043#include <xen/events.h>
44#include <xen/page.h>
Stefano Stabellinie79affc2012-08-08 17:21:14 +000045#include <xen/xen.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080046#include <asm/xen/hypervisor.h>
47#include <asm/xen/hypercall.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040048#include "common.h"
49
50/*
51 * These are rather arbitrary. They are fairly large because adjacent requests
52 * pulled from a communication ring are quite likely to end up being part of
53 * the same scatter/gather request at the disc.
54 *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040055 * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040056 *
57 * This will increase the chances of being able to write whole tracks.
58 * 64 should be enough to keep us competitive with Linux.
59 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040060static int xen_blkif_reqs = 64;
61module_param_named(reqs, xen_blkif_reqs, int, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040062MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
63
64/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040065static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040066module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040067
68/*
69 * Each outstanding request that we've passed to the lower device layers has a
70 * 'pending_req' allocated to it. Each buffer_head that completes decrements
71 * the pendcnt towards zero. When it hits zero, the specified domain has a
72 * response queued for it, with the saved 'id' passed back.
73 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040074struct pending_req {
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040075 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040076 u64 id;
77 int nr_pages;
78 atomic_t pendcnt;
79 unsigned short operation;
80 int status;
81 struct list_head free_list;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040082};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040083
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040084#define BLKBACK_INVALID_HANDLE (~0)
85
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050086struct xen_blkbk {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040087 struct pending_req *pending_reqs;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040088 /* List of all 'pending_req' available */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050089 struct list_head pending_free;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040090 /* And its spinlock. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050091 spinlock_t pending_free_lock;
92 wait_queue_head_t pending_free_wq;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040093 /* The list of all pages that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050094 struct page **pending_pages;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040095 /* And the grant handles that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050096 grant_handle_t *pending_grant_handles;
97};
98
99static struct xen_blkbk *blkbk;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400100
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400101/*
102 * Little helpful macro to figure out the index and virtual address of the
103 * pending_pages[..]. For each 'pending_req' we have have up to
104 * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400105 * 10 and would index in the pending_pages[..].
106 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400107static inline int vaddr_pagenr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400108{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400109 return (req - blkbk->pending_reqs) *
110 BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400111}
112
Jan Beulichefe08a32010-02-05 14:19:33 -0500113#define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
114
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400115static inline unsigned long vaddr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400116{
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500117 unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400118 return (unsigned long)pfn_to_kaddr(pfn);
119}
120
121#define pending_handle(_req, _seg) \
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500122 (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400123
124
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400125static int do_block_io_op(struct xen_blkif *blkif);
126static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400127 struct blkif_request *req,
128 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400129static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400130 unsigned short op, int st);
131
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400132/*
133 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400134 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400135static struct pending_req *alloc_req(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400136{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400137 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400138 unsigned long flags;
139
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500140 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
141 if (!list_empty(&blkbk->pending_free)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400142 req = list_entry(blkbk->pending_free.next, struct pending_req,
143 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400144 list_del(&req->free_list);
145 }
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500146 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400147 return req;
148}
149
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400150/*
151 * Return the 'pending_req' structure back to the freepool. We also
152 * wake up the thread if it was waiting for a free page.
153 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400154static void free_req(struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400155{
156 unsigned long flags;
157 int was_empty;
158
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500159 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
160 was_empty = list_empty(&blkbk->pending_free);
161 list_add(&req->free_list, &blkbk->pending_free);
162 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400163 if (was_empty)
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500164 wake_up(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400165}
166
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400167/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400168 * Routines for managing virtual block devices (vbds).
169 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400170static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
171 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400172{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400173 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400174 int rc = -EACCES;
175
176 if ((operation != READ) && vbd->readonly)
177 goto out;
178
Jan Beulich8ab52152011-05-17 11:07:05 +0100179 if (likely(req->nr_sects)) {
180 blkif_sector_t end = req->sector_number + req->nr_sects;
181
182 if (unlikely(end < req->sector_number))
183 goto out;
184 if (unlikely(end > vbd_sz(vbd)))
185 goto out;
186 }
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400187
188 req->dev = vbd->pdevice;
189 req->bdev = vbd->bdev;
190 rc = 0;
191
192 out:
193 return rc;
194}
195
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400196static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400197{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400198 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400199 struct xenbus_transaction xbt;
200 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400201 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400202 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400203
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400204 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400205 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400206 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400207 vbd->size = new_size;
208again:
209 err = xenbus_transaction_start(&xbt);
210 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400211 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400212 return;
213 }
214 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400215 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400216 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400217 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400218 goto abort;
219 }
220 /*
221 * Write the current state; we will use this to synchronize
222 * the front-end. If the current state is "connected" the
223 * front-end will get the new size information online.
224 */
225 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
226 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400227 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400228 goto abort;
229 }
230
231 err = xenbus_transaction_end(xbt, 0);
232 if (err == -EAGAIN)
233 goto again;
234 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400235 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400236 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400237abort:
238 xenbus_transaction_end(xbt, 1);
239}
240
241/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400242 * Notification from the guest OS.
243 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400244static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400245{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400246 blkif->waiting_reqs = 1;
247 wake_up(&blkif->wq);
248}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400249
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400250irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400251{
252 blkif_notify_work(dev_id);
253 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400254}
255
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400256/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400257 * SCHEDULER FUNCTIONS
258 */
259
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400260static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400261{
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800262 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
263 " | ds %4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400264 current->comm, blkif->st_oo_req,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800265 blkif->st_rd_req, blkif->st_wr_req,
266 blkif->st_f_req, blkif->st_ds_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400267 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
268 blkif->st_rd_req = 0;
269 blkif->st_wr_req = 0;
270 blkif->st_oo_req = 0;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800271 blkif->st_ds_req = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400272}
273
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400274int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400275{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400276 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400277 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400278
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400279 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400280
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400281 while (!kthread_should_stop()) {
282 if (try_to_freeze())
283 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400284 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400285 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400286
287 wait_event_interruptible(
288 blkif->wq,
289 blkif->waiting_reqs || kthread_should_stop());
290 wait_event_interruptible(
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500291 blkbk->pending_free_wq,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400292 !list_empty(&blkbk->pending_free) ||
293 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400294
295 blkif->waiting_reqs = 0;
296 smp_mb(); /* clear flag *before* checking for work */
297
298 if (do_block_io_op(blkif))
299 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400300
301 if (log_stats && time_after(jiffies, blkif->st_print))
302 print_stats(blkif);
303 }
304
305 if (log_stats)
306 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400307
308 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400309 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400310
311 return 0;
312}
313
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400314struct seg_buf {
315 unsigned long buf;
316 unsigned int nsec;
317};
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400318/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400319 * Unmap the grant references, and also remove the M2P over-rides
320 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400321 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400322static void xen_blkbk_unmap(struct pending_req *req)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400323{
324 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500325 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400326 unsigned int i, invcount = 0;
327 grant_handle_t handle;
328 int ret;
329
330 for (i = 0; i < req->nr_pages; i++) {
331 handle = pending_handle(req, i);
332 if (handle == BLKBACK_INVALID_HANDLE)
333 continue;
334 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
335 GNTMAP_host_map, handle);
336 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500337 pages[invcount] = virt_to_page(vaddr(req, i));
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400338 invcount++;
339 }
340
Stefano Stabellini2fc136e2012-09-12 12:44:30 +0100341 ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400342 BUG_ON(ret);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400343}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400344
345static int xen_blkbk_map(struct blkif_request *req,
346 struct pending_req *pending_req,
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400347 struct seg_buf seg[])
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400348{
349 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
350 int i;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400351 int nseg = req->u.rw.nr_segments;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400352 int ret = 0;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400353
354 /*
355 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400356 * assign map[..] with the PFN of the page in our domain with the
357 * corresponding grant reference for each page.
358 */
359 for (i = 0; i < nseg; i++) {
360 uint32_t flags;
361
362 flags = GNTMAP_host_map;
363 if (pending_req->operation != BLKIF_OP_READ)
364 flags |= GNTMAP_readonly;
365 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400366 req->u.rw.seg[i].gref,
367 pending_req->blkif->domid);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400368 }
369
Daniel De Graaf4f14faa2011-11-28 11:49:03 -0500370 ret = gnttab_map_refs(map, NULL, &blkbk->pending_page(pending_req, 0), nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400371 BUG_ON(ret);
372
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400373 /*
374 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400375 * so that when we access vaddr(pending_req,i) it has the contents of
376 * the page from the other domain.
377 */
378 for (i = 0; i < nseg; i++) {
379 if (unlikely(map[i].status != 0)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400380 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400381 map[i].handle = BLKBACK_INVALID_HANDLE;
382 ret |= 1;
383 }
384
385 pending_handle(pending_req, i) = map[i].handle;
386
387 if (ret)
388 continue;
389
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400390 seg[i].buf = map[i].dev_bus_addr |
391 (req->u.rw.seg[i].first_sect << 9);
392 }
393 return ret;
394}
395
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400396static int dispatch_discard_io(struct xen_blkif *blkif,
397 struct blkif_request *req)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800398{
399 int err = 0;
400 int status = BLKIF_RSP_OKAY;
401 struct block_device *bdev = blkif->vbd.bdev;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400402 unsigned long secure;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800403
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400404 blkif->st_ds_req++;
405
406 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400407 secure = (blkif->vbd.discard_secure &&
408 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
409 BLKDEV_DISCARD_SECURE : 0;
410
411 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
412 req->u.discard.nr_sectors,
413 GFP_KERNEL, secure);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800414
415 if (err == -EOPNOTSUPP) {
416 pr_debug(DRV_PFX "discard op failed, not supported\n");
417 status = BLKIF_RSP_EOPNOTSUPP;
418 } else if (err)
419 status = BLKIF_RSP_ERROR;
420
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400421 make_response(blkif, req->u.discard.id, req->operation, status);
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400422 xen_blkif_put(blkif);
423 return err;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800424}
425
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400426static void xen_blk_drain_io(struct xen_blkif *blkif)
427{
428 atomic_set(&blkif->drain, 1);
429 do {
Konrad Rzeszutek Wilk6927d922011-10-17 14:27:48 -0400430 /* The initial value is one, and one refcnt taken at the
431 * start of the xen_blkif_schedule thread. */
432 if (atomic_read(&blkif->refcnt) <= 2)
433 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400434 wait_for_completion_interruptible_timeout(
435 &blkif->drain_complete, HZ);
436
437 if (!atomic_read(&blkif->drain))
438 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400439 } while (!kthread_should_stop());
440 atomic_set(&blkif->drain, 0);
441}
442
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400443/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400444 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400445 */
446
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400447static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400448{
449 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400450 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400451 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400452 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400453 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400454 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400455 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
456 (error == -EOPNOTSUPP)) {
457 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
458 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
459 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400460 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400461 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400462 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400463 pending_req->status = BLKIF_RSP_ERROR;
464 }
465
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400466 /*
467 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400468 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400469 * the proper response on the ring.
470 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400471 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400472 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400473 make_response(pending_req->blkif, pending_req->id,
474 pending_req->operation, pending_req->status);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400475 xen_blkif_put(pending_req->blkif);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400476 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
477 if (atomic_read(&pending_req->blkif->drain))
478 complete(&pending_req->blkif->drain_complete);
479 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400480 free_req(pending_req);
481 }
482}
483
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400484/*
485 * bio callback.
486 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800487static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400488{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400489 __end_block_io_op(bio->bi_private, error);
490 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400491}
492
493
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400494
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400495/*
496 * Function to copy the from the ring buffer the 'struct blkif_request'
497 * (which has the sectors we want, number of them, grant references, etc),
498 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400499 */
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700500static int
501__do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400502{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800503 union blkif_back_rings *blk_rings = &blkif->blk_rings;
504 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400505 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400506 RING_IDX rc, rp;
507 int more_to_do = 0;
508
509 rc = blk_rings->common.req_cons;
510 rp = blk_rings->common.sring->req_prod;
511 rmb(); /* Ensure we see queued requests up to 'rp'. */
512
513 while (rc != rp) {
514
515 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
516 break;
517
Keir Fraser8270b452009-03-06 08:29:15 +0000518 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400519 more_to_do = 1;
520 break;
521 }
522
Keir Fraser8270b452009-03-06 08:29:15 +0000523 pending_req = alloc_req();
524 if (NULL == pending_req) {
525 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400526 more_to_do = 1;
527 break;
528 }
529
530 switch (blkif->blk_protocol) {
531 case BLKIF_PROTOCOL_NATIVE:
532 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
533 break;
534 case BLKIF_PROTOCOL_X86_32:
535 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
536 break;
537 case BLKIF_PROTOCOL_X86_64:
538 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
539 break;
540 default:
541 BUG();
542 }
543 blk_rings->common.req_cons = ++rc; /* before make_response() */
544
545 /* Apply all sanity checks to /private copy/ of request. */
546 barrier();
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400547 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
548 free_req(pending_req);
549 if (dispatch_discard_io(blkif, &req))
550 break;
551 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400552 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400553
554 /* Yield point for this unbounded loop. */
555 cond_resched();
556 }
557
558 return more_to_do;
559}
560
Daniel Stoddenb4726a92011-05-28 13:21:10 -0700561static int
562do_block_io_op(struct xen_blkif *blkif)
563{
564 union blkif_back_rings *blk_rings = &blkif->blk_rings;
565 int more_to_do;
566
567 do {
568 more_to_do = __do_block_io_op(blkif);
569 if (more_to_do)
570 break;
571
572 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
573 } while (more_to_do);
574
575 return more_to_do;
576}
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400577/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400578 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
579 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400580 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400581static int dispatch_rw_block_io(struct xen_blkif *blkif,
582 struct blkif_request *req,
583 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400584{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400585 struct phys_req preq;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400586 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400587 unsigned int nseg;
588 struct bio *bio = NULL;
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400589 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400590 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400591 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400592 struct blk_plug plug;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400593 bool drain = false;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400594
595 switch (req->operation) {
596 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400597 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400598 operation = READ;
599 break;
600 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400601 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -0400602 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400603 break;
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400604 case BLKIF_OP_WRITE_BARRIER:
605 drain = true;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400606 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400607 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400608 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400609 break;
610 default:
611 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400612 goto fail_response;
613 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400614 }
615
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400616 /* Check that the number of segments is sane. */
617 nseg = req->u.rw.nr_segments;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400618
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400619 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400620 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400621 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400622 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400623 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400624 goto fail_response;
625 }
626
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400627 preq.dev = req->u.rw.handle;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500628 preq.sector_number = req->u.rw.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400629 preq.nr_sects = 0;
630
631 pending_req->blkif = blkif;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400632 pending_req->id = req->u.rw.id;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400633 pending_req->operation = req->operation;
634 pending_req->status = BLKIF_RSP_OKAY;
635 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400636
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400637 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500638 seg[i].nsec = req->u.rw.seg[i].last_sect -
639 req->u.rw.seg[i].first_sect + 1;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500640 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
641 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400642 goto fail_response;
643 preq.nr_sects += seg[i].nsec;
Konrad Rzeszutek Wilk976222e2011-04-15 11:38:29 -0400644
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400645 }
646
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400647 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400648 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400649 operation == READ ? "read" : "write",
650 preq.sector_number,
651 preq.sector_number + preq.nr_sects, preq.dev);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400652 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400653 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400654
655 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400656 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400657 * is set there.
658 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400659 for (i = 0; i < nseg; i++) {
660 if (((int)preq.sector_number|(int)seg[i].nsec) &
661 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400662 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400663 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400664 goto fail_response;
665 }
666 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400667
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400668 /* Wait on all outstanding I/O's and once that has been completed
669 * issue the WRITE_FLUSH.
670 */
671 if (drain)
672 xen_blk_drain_io(pending_req->blkif);
673
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400674 /*
675 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400676 * set gnttab_set_unmap_op on all of the grant references and perform
677 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400678 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400679 */
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400680 if (xen_blkbk_map(req, pending_req, seg))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400681 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400682
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800683 /*
684 * This corresponding xen_blkif_put is done in __end_block_io_op, or
685 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
686 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400687 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400688
689 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400690 while ((bio == NULL) ||
691 (bio_add_page(bio,
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500692 blkbk->pending_page(pending_req, i),
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400693 seg[i].nsec << 9,
694 seg[i].buf & ~PAGE_MASK) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400695
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400696 bio = bio_alloc(GFP_KERNEL, nseg-i);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400697 if (unlikely(bio == NULL))
698 goto fail_put_bio;
699
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400700 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400701 bio->bi_bdev = preq.bdev;
702 bio->bi_private = pending_req;
703 bio->bi_end_io = end_block_io_op;
704 bio->bi_sector = preq.sector_number;
705 }
706
707 preq.sector_number += seg[i].nsec;
708 }
709
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800710 /* This will be hit if the operation was a flush or discard. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400711 if (!bio) {
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400712 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -0400713
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400714 bio = bio_alloc(GFP_KERNEL, 0);
715 if (unlikely(bio == NULL))
716 goto fail_put_bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400717
Konrad Rzeszutek Wilk42146352011-10-12 17:26:47 -0400718 biolist[nbio++] = bio;
719 bio->bi_bdev = preq.bdev;
720 bio->bi_private = pending_req;
721 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400722 }
723
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400724 /*
725 * We set it one so that the last submit_bio does not have to call
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400726 * atomic_inc.
727 */
728 atomic_set(&pending_req->pendcnt, nbio);
729
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400730 /* Get a reference count for the disk queue and start sending I/O */
731 blk_start_plug(&plug);
732
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400733 for (i = 0; i < nbio; i++)
734 submit_bio(operation, biolist[i]);
735
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400736 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400737 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400738
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739 if (operation == READ)
740 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk5c62cb42011-10-10 12:33:21 -0400741 else if (operation & WRITE)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400742 blkif->st_wr_sect += preq.nr_sects;
743
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400744 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400745
746 fail_flush:
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400747 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -0400749 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400750 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400751 free_req(pending_req);
752 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400753 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400754
755 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400756 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400757 bio_put(biolist[i]);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400758 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400759 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400760 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400761}
762
763
764
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400765/*
766 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400767 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400768static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400769 unsigned short op, int st)
770{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800771 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400772 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800773 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400774 int notify;
775
776 resp.id = id;
777 resp.operation = op;
778 resp.status = st;
779
780 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
781 /* Place on the response ring for the relevant domain. */
782 switch (blkif->blk_protocol) {
783 case BLKIF_PROTOCOL_NATIVE:
784 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
785 &resp, sizeof(resp));
786 break;
787 case BLKIF_PROTOCOL_X86_32:
788 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
789 &resp, sizeof(resp));
790 break;
791 case BLKIF_PROTOCOL_X86_64:
792 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
793 &resp, sizeof(resp));
794 break;
795 default:
796 BUG();
797 }
798 blk_rings->common.rsp_prod_pvt++;
799 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400800 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400801 if (notify)
802 notify_remote_via_irq(blkif->irq);
803}
804
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400805static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400806{
807 int i, mmap_pages;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400808 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400809
Daniel De Graafb2167ba2011-11-28 11:49:05 -0500810 if (!xen_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400811 return -ENODEV;
812
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400813 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500814 if (!blkbk) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400815 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500816 return -ENOMEM;
817 }
818
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400819 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400820
Jan Beulich8e6dc6f2011-09-16 08:38:09 +0100821 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400822 xen_blkif_reqs, GFP_KERNEL);
Jan Beulich8e6dc6f2011-09-16 08:38:09 +0100823 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400824 mmap_pages, GFP_KERNEL);
825 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
826 mmap_pages, GFP_KERNEL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400827
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400828 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
829 !blkbk->pending_pages) {
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400830 rc = -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400831 goto out_of_memory;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400832 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400833
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500834 for (i = 0; i < mmap_pages; i++) {
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500835 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400836 blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500837 if (blkbk->pending_pages[i] == NULL) {
838 rc = -ENOMEM;
839 goto out_of_memory;
840 }
841 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400842 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400843 if (rc)
844 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400845
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500846 INIT_LIST_HEAD(&blkbk->pending_free);
847 spin_lock_init(&blkbk->pending_free_lock);
848 init_waitqueue_head(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400849
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400850 for (i = 0; i < xen_blkif_reqs; i++)
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400851 list_add_tail(&blkbk->pending_reqs[i].free_list,
852 &blkbk->pending_free);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400853
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400854 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400855 if (rc)
856 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400857
858 return 0;
859
860 out_of_memory:
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400861 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400862 failed_init:
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500863 kfree(blkbk->pending_reqs);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400864 kfree(blkbk->pending_grant_handles);
Dan Carpenter9b83c772011-05-27 09:27:16 +0300865 if (blkbk->pending_pages) {
866 for (i = 0; i < mmap_pages; i++) {
867 if (blkbk->pending_pages[i])
868 __free_page(blkbk->pending_pages[i]);
869 }
870 kfree(blkbk->pending_pages);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500871 }
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400872 kfree(blkbk);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500873 blkbk = NULL;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400874 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400875}
876
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400877module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400878
879MODULE_LICENSE("Dual BSD/GPL");
Bastian Blanka7e93572011-06-29 14:40:50 +0200880MODULE_ALIAS("xen-backend:vbd");