blob: c3dae2e0f290e8ad64b4f3e6c869c2fca14cd1a1 [file] [log] [blame]
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001/*
2 * blkfront.c
3 *
4 * XenLinux virtual block device driver.
5 *
6 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
7 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
8 * Copyright (c) 2004, Christian Limpach
9 * Copyright (c) 2004, Andrew Warfield
10 * Copyright (c) 2005, Christopher Clark
11 * Copyright (c) 2005, XenSource Ltd
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation; or, when distributed
16 * separately from the Linux kernel or incorporated into other
17 * software packages, subject to the following license:
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this source file (the "Software"), to deal in the Software without
21 * restriction, including without limitation the rights to use, copy, modify,
22 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
23 * and to permit persons to whom the Software is furnished to do so, subject to
24 * the following conditions:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 * IN THE SOFTWARE.
36 */
37
38#include <linux/interrupt.h>
39#include <linux/blkdev.h>
Ian Campbell597592d2008-02-21 13:03:45 -080040#include <linux/hdreg.h>
Christian Limpach440a01a2008-06-17 10:47:08 +020041#include <linux/cdrom.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070042#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020044#include <linux/mutex.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010045#include <linux/scatterlist.h>
Akinobu Mita34ae2e42012-01-21 00:15:26 +090046#include <linux/bitmap.h>
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020047#include <linux/llist.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070049#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070050#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010054#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070055
56#include <xen/interface/grant_table.h>
57#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070058#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070059
60#include <asm/xen/hypervisor.h>
61
62enum blkif_state {
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
66};
67
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020068struct grant {
69 grant_ref_t gref;
70 unsigned long pfn;
71 struct llist_node node;
72};
73
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074struct blk_shadow {
75 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040076 struct request *request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070077 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020078 struct grant *grants_used[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070079};
80
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020081static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070082static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070083
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080084#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085
86/*
87 * We have one of these per vbd, whether ide, scsi or 'other'. They
88 * hang in private_data off the gendisk structure. We may end up
89 * putting all kinds of interesting stuff here :-)
90 */
91struct blkfront_info
92{
Steven Noonan34678112012-02-17 12:04:44 -080093 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000094 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070095 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070096 struct gendisk *gd;
97 int vdevice;
98 blkif_vdev_t handle;
99 enum blkif_state connected;
100 int ring_ref;
101 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +0100102 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700103 unsigned int evtchn, irq;
104 struct request_queue *rq;
105 struct work_struct work;
106 struct gnttab_free_callback callback;
107 struct blk_shadow shadow[BLK_RING_SIZE];
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200108 struct llist_head persistent_gnts;
109 unsigned int persistent_gnts_c;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700110 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200111 unsigned int feature_flush;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400112 unsigned int flush_op;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400113 unsigned int feature_discard:1;
114 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800115 unsigned int discard_granularity;
116 unsigned int discard_alignment;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200117 unsigned int feature_persistent:1;
Christian Limpach1d78d702008-04-02 10:54:04 -0700118 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700119};
120
Jan Beulich0e345822010-08-07 18:28:55 +0200121static unsigned int nr_minors;
122static unsigned long *minors;
123static DEFINE_SPINLOCK(minor_lock);
124
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700125#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
126 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
127#define GRANT_INVALID_REF 0
128
129#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700130#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700131
132#define BLKIF_MAJOR(dev) ((dev)>>8)
133#define BLKIF_MINOR(dev) ((dev) & 0xff)
134
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700135#define EXT_SHIFT 28
136#define EXTENDED (1<<EXT_SHIFT)
137#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
138#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000139#define EMULATED_HD_DISK_MINOR_OFFSET (0)
140#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200141#define EMULATED_SD_DISK_MINOR_OFFSET (0)
142#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700143
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700144#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700145
146static int get_id_from_freelist(struct blkfront_info *info)
147{
148 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200149 BUG_ON(free >= BLK_RING_SIZE);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400150 info->shadow_free = info->shadow[free].req.u.rw.id;
151 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700152 return free;
153}
154
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400155static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700156 unsigned long id)
157{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400158 if (info->shadow[id].req.u.rw.id != id)
159 return -EINVAL;
160 if (info->shadow[id].request == NULL)
161 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400162 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400163 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700164 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400165 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700166}
167
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400168static const char *op_name(int op)
169{
170 static const char *const names[] = {
171 [BLKIF_OP_READ] = "read",
172 [BLKIF_OP_WRITE] = "write",
173 [BLKIF_OP_WRITE_BARRIER] = "barrier",
174 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
175 [BLKIF_OP_DISCARD] = "discard" };
176
177 if (op < 0 || op >= ARRAY_SIZE(names))
178 return "unknown";
179
180 if (!names[op])
181 return "reserved";
182
183 return names[op];
184}
Jan Beulich0e345822010-08-07 18:28:55 +0200185static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
186{
187 unsigned int end = minor + nr;
188 int rc;
189
190 if (end > nr_minors) {
191 unsigned long *bitmap, *old;
192
Thomas Meyerf0941482011-11-29 22:08:00 +0100193 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200194 GFP_KERNEL);
195 if (bitmap == NULL)
196 return -ENOMEM;
197
198 spin_lock(&minor_lock);
199 if (end > nr_minors) {
200 old = minors;
201 memcpy(bitmap, minors,
202 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
203 minors = bitmap;
204 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
205 } else
206 old = bitmap;
207 spin_unlock(&minor_lock);
208 kfree(old);
209 }
210
211 spin_lock(&minor_lock);
212 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900213 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200214 rc = 0;
215 } else
216 rc = -EBUSY;
217 spin_unlock(&minor_lock);
218
219 return rc;
220}
221
222static void xlbd_release_minors(unsigned int minor, unsigned int nr)
223{
224 unsigned int end = minor + nr;
225
226 BUG_ON(end > nr_minors);
227 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900228 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200229 spin_unlock(&minor_lock);
230}
231
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700232static void blkif_restart_queue_callback(void *arg)
233{
234 struct blkfront_info *info = (struct blkfront_info *)arg;
235 schedule_work(&info->work);
236}
237
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700238static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800239{
240 /* We don't have real geometry info, but let's at least return
241 values consistent with the size of the device */
242 sector_t nsect = get_capacity(bd->bd_disk);
243 sector_t cylinders = nsect;
244
245 hg->heads = 0xff;
246 hg->sectors = 0x3f;
247 sector_div(cylinders, hg->heads * hg->sectors);
248 hg->cylinders = cylinders;
249 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
250 hg->cylinders = 0xffff;
251 return 0;
252}
253
Al Viroa63c8482008-03-02 10:23:47 -0500254static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200255 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200256{
Al Viroa63c8482008-03-02 10:23:47 -0500257 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200258 int i;
259
260 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
261 command, (long)argument);
262
263 switch (command) {
264 case CDROMMULTISESSION:
265 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
266 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
267 if (put_user(0, (char __user *)(argument + i)))
268 return -EFAULT;
269 return 0;
270
271 case CDROM_GET_CAPABILITY: {
272 struct gendisk *gd = info->gd;
273 if (gd->flags & GENHD_FL_CD)
274 return 0;
275 return -EINVAL;
276 }
277
278 default:
279 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
280 command);*/
281 return -EINVAL; /* same return as native Linux */
282 }
283
284 return 0;
285}
286
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700287/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400288 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400289 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700290 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400291 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700292 */
293static int blkif_queue_request(struct request *req)
294{
295 struct blkfront_info *info = req->rq_disk->private_data;
296 unsigned long buffer_mfn;
297 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700298 unsigned long id;
299 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100300 int i, ref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200301
302 /*
303 * Used to store if we are able to queue the request by just using
304 * existing persistent grants, or if we have to get new grants,
305 * as there are not sufficiently many free.
306 */
307 bool new_persistent_gnts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700308 grant_ref_t gref_head;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200309 struct page *granted_page;
310 struct grant *gnt_list_entry = NULL;
Jens Axboe9e973e62009-02-24 08:10:09 +0100311 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700312
313 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
314 return 1;
315
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200316 /* Check if we have enought grants to allocate a requests */
317 if (info->persistent_gnts_c < BLKIF_MAX_SEGMENTS_PER_REQUEST) {
318 new_persistent_gnts = 1;
319 if (gnttab_alloc_grant_references(
320 BLKIF_MAX_SEGMENTS_PER_REQUEST - info->persistent_gnts_c,
321 &gref_head) < 0) {
322 gnttab_request_free_callback(
323 &info->callback,
324 blkif_restart_queue_callback,
325 info,
326 BLKIF_MAX_SEGMENTS_PER_REQUEST);
327 return 1;
328 }
329 } else
330 new_persistent_gnts = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700331
332 /* Fill out a communications ring structure. */
333 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
334 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400335 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700336
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400337 ring_req->u.rw.id = id;
Owen Smith51de6952010-12-22 15:05:00 +0000338 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400339 ring_req->u.rw.handle = info->handle;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700340
341 ring_req->operation = rq_data_dir(req) ?
342 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400343
344 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
345 /*
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400346 * Ideally we can do an unordered flush-to-disk. In case the
347 * backend onlysupports barriers, use that. A barrier request
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400348 * a superset of FUA, so we can implement it the same
349 * way. (It's also a FLUSH+FUA, since it is
350 * guaranteed ordered WRT previous writes.)
351 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400352 ring_req->operation = info->flush_op;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400353 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700354
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400355 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800356 /* id, sector_number and handle are set above. */
357 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800358 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400359 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
360 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
361 else
362 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800363 } else {
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400364 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
365 info->sg);
366 BUG_ON(ring_req->u.rw.nr_segments >
367 BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jens Axboe9e973e62009-02-24 08:10:09 +0100368
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400369 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800370 fsect = sg->offset >> 9;
371 lsect = fsect + (sg->length >> 9) - 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200373 if (info->persistent_gnts_c) {
374 BUG_ON(llist_empty(&info->persistent_gnts));
375 gnt_list_entry = llist_entry(
376 llist_del_first(&info->persistent_gnts),
377 struct grant, node);
378
379 ref = gnt_list_entry->gref;
380 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
381 info->persistent_gnts_c--;
382 } else {
383 ref = gnttab_claim_grant_reference(&gref_head);
384 BUG_ON(ref == -ENOSPC);
385
386 gnt_list_entry =
387 kmalloc(sizeof(struct grant),
388 GFP_ATOMIC);
389 if (!gnt_list_entry)
390 return -ENOMEM;
391
392 granted_page = alloc_page(GFP_ATOMIC);
393 if (!granted_page) {
394 kfree(gnt_list_entry);
395 return -ENOMEM;
396 }
397
398 gnt_list_entry->pfn =
399 page_to_pfn(granted_page);
400 gnt_list_entry->gref = ref;
401
402 buffer_mfn = pfn_to_mfn(page_to_pfn(
403 granted_page));
404 gnttab_grant_foreign_access_ref(ref,
Li Dongyanged30bf32011-09-01 18:39:09 +0800405 info->xbdev->otherend_id,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200406 buffer_mfn, 0);
407 }
408
409 info->shadow[id].grants_used[i] = gnt_list_entry;
410
411 if (rq_data_dir(req)) {
412 char *bvec_data;
413 void *shared_data;
414
415 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
416
417 shared_data = kmap_atomic(
418 pfn_to_page(gnt_list_entry->pfn));
419 bvec_data = kmap_atomic(sg_page(sg));
420
421 /*
422 * this does not wipe data stored outside the
423 * range sg->offset..sg->offset+sg->length.
424 * Therefore, blkback *could* see data from
425 * previous requests. This is OK as long as
426 * persistent grants are shared with just one
427 * domain. It may need refactoring if this
428 * changes
429 */
430 memcpy(shared_data + sg->offset,
431 bvec_data + sg->offset,
432 sg->length);
433
434 kunmap_atomic(bvec_data);
435 kunmap_atomic(shared_data);
436 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700437
Li Dongyanged30bf32011-09-01 18:39:09 +0800438 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
439 ring_req->u.rw.seg[i] =
440 (struct blkif_request_segment) {
441 .gref = ref,
442 .first_sect = fsect,
443 .last_sect = lsect };
444 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700445 }
446
447 info->ring.req_prod_pvt++;
448
449 /* Keep a private copy so we can reissue requests when recovering. */
450 info->shadow[id].req = *ring_req;
451
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200452 if (new_persistent_gnts)
453 gnttab_free_grant_references(gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700454
455 return 0;
456}
457
458
459static inline void flush_requests(struct blkfront_info *info)
460{
461 int notify;
462
463 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
464
465 if (notify)
466 notify_remote_via_irq(info->irq);
467}
468
469/*
470 * do_blkif_request
471 * read a block; request is in a request queue
472 */
Jens Axboe165125e2007-07-24 09:28:11 +0200473static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700474{
475 struct blkfront_info *info = NULL;
476 struct request *req;
477 int queued;
478
479 pr_debug("Entered do_blkif_request\n");
480
481 queued = 0;
482
Tejun Heo9934c8c2009-05-08 11:54:16 +0900483 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700484 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700485
486 if (RING_FULL(&info->ring))
487 goto wait;
488
Tejun Heo9934c8c2009-05-08 11:54:16 +0900489 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900490
Konrad Rzeszutek Wilkd11e6152011-09-16 15:15:14 -0400491 if ((req->cmd_type != REQ_TYPE_FS) ||
492 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
493 !info->flush_op)) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900494 __blk_end_request_all(req, -EIO);
495 continue;
496 }
497
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700498 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900499 "(%u/%u) buffer:%p [%s]\n",
500 req, req->cmd, (unsigned long)blk_rq_pos(req),
501 blk_rq_cur_sectors(req), blk_rq_sectors(req),
502 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700503
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700504 if (blkif_queue_request(req)) {
505 blk_requeue_request(rq, req);
506wait:
507 /* Avoid pointless unplugs. */
508 blk_stop_queue(rq);
509 break;
510 }
511
512 queued++;
513 }
514
515 if (queued != 0)
516 flush_requests(info);
517}
518
519static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
520{
Jens Axboe165125e2007-07-24 09:28:11 +0200521 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800522 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700523
Steven Noonan34678112012-02-17 12:04:44 -0800524 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700525 if (rq == NULL)
526 return -1;
527
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900528 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700529
Li Dongyanged30bf32011-09-01 18:39:09 +0800530 if (info->feature_discard) {
531 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
532 blk_queue_max_discard_sectors(rq, get_capacity(gd));
533 rq->limits.discard_granularity = info->discard_granularity;
534 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400535 if (info->feature_secdiscard)
536 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800537 }
538
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700539 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400540 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500541 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700542
543 /* Each segment in a request is up to an aligned page in size. */
544 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
545 blk_queue_max_segment_size(rq, PAGE_SIZE);
546
547 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500548 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700549
550 /* Make sure buffer addresses are sector-aligned. */
551 blk_queue_dma_alignment(rq, 511);
552
Ian Campbell1c91fe12008-06-17 10:47:08 +0200553 /* Make sure we don't use bounce buffers. */
554 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
555
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700556 gd->queue = rq;
557
558 return 0;
559}
560
561
Tejun Heo4913efe2010-09-03 11:56:16 +0200562static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700563{
Tejun Heo4913efe2010-09-03 11:56:16 +0200564 blk_queue_flush(info->rq, info->feature_flush);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200565 printk(KERN_INFO "blkfront: %s: %s: %s %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200566 info->gd->disk_name,
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400567 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
568 "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
569 "flush diskcache" : "barrier or flush"),
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200570 info->feature_flush ? "enabled" : "disabled",
571 info->feature_persistent ? "using persistent grants" : "");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700572}
573
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000574static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
575{
576 int major;
577 major = BLKIF_MAJOR(vdevice);
578 *minor = BLKIF_MINOR(vdevice);
579 switch (major) {
580 case XEN_IDE0_MAJOR:
581 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
582 *minor = ((*minor / 64) * PARTS_PER_DISK) +
583 EMULATED_HD_DISK_MINOR_OFFSET;
584 break;
585 case XEN_IDE1_MAJOR:
586 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
587 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
588 EMULATED_HD_DISK_MINOR_OFFSET;
589 break;
590 case XEN_SCSI_DISK0_MAJOR:
591 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
592 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
593 break;
594 case XEN_SCSI_DISK1_MAJOR:
595 case XEN_SCSI_DISK2_MAJOR:
596 case XEN_SCSI_DISK3_MAJOR:
597 case XEN_SCSI_DISK4_MAJOR:
598 case XEN_SCSI_DISK5_MAJOR:
599 case XEN_SCSI_DISK6_MAJOR:
600 case XEN_SCSI_DISK7_MAJOR:
601 *offset = (*minor / PARTS_PER_DISK) +
602 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
603 EMULATED_SD_DISK_NAME_OFFSET;
604 *minor = *minor +
605 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
606 EMULATED_SD_DISK_MINOR_OFFSET;
607 break;
608 case XEN_SCSI_DISK8_MAJOR:
609 case XEN_SCSI_DISK9_MAJOR:
610 case XEN_SCSI_DISK10_MAJOR:
611 case XEN_SCSI_DISK11_MAJOR:
612 case XEN_SCSI_DISK12_MAJOR:
613 case XEN_SCSI_DISK13_MAJOR:
614 case XEN_SCSI_DISK14_MAJOR:
615 case XEN_SCSI_DISK15_MAJOR:
616 *offset = (*minor / PARTS_PER_DISK) +
617 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
618 EMULATED_SD_DISK_NAME_OFFSET;
619 *minor = *minor +
620 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
621 EMULATED_SD_DISK_MINOR_OFFSET;
622 break;
623 case XENVBD_MAJOR:
624 *offset = *minor / PARTS_PER_DISK;
625 break;
626 default:
627 printk(KERN_WARNING "blkfront: your disk configuration is "
628 "incorrect, please use an xvd device instead\n");
629 return -ENODEV;
630 }
631 return 0;
632}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700633
Jan Beuliche77c78c2012-04-05 16:37:22 +0100634static char *encode_disk_name(char *ptr, unsigned int n)
635{
636 if (n >= 26)
637 ptr = encode_disk_name(ptr, n / 26 - 1);
638 *ptr = 'a' + n % 26;
639 return ptr + 1;
640}
641
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700642static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
643 struct blkfront_info *info,
644 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700645{
646 struct gendisk *gd;
647 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000648 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700649 unsigned int offset;
650 int minor;
651 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100652 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700653
654 BUG_ON(info->gd != NULL);
655 BUG_ON(info->rq != NULL);
656
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700657 if ((info->vdevice>>EXT_SHIFT) > 1) {
658 /* this is above the extended range; something is wrong */
659 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
660 return -ENODEV;
661 }
662
663 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000664 err = xen_translate_vdev(info->vdevice, &minor, &offset);
665 if (err)
666 return err;
667 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700668 } else {
669 minor = BLKIF_MINOR_EXT(info->vdevice);
670 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000671 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200672 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000673 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
674 "emulated IDE disks,\n\t choose an xvd device name"
675 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700676 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100677 if (minor >> MINORBITS) {
678 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
679 info->vdevice, minor);
680 return -ENODEV;
681 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700682
683 if ((minor % nr_parts) == 0)
684 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700685
Jan Beulich0e345822010-08-07 18:28:55 +0200686 err = xlbd_reserve_minors(minor, nr_minors);
687 if (err)
688 goto out;
689 err = -ENODEV;
690
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700691 gd = alloc_disk(nr_minors);
692 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200693 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700694
Jan Beuliche77c78c2012-04-05 16:37:22 +0100695 strcpy(gd->disk_name, DEV_NAME);
696 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
697 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
698 if (nr_minors > 1)
699 *ptr = 0;
700 else
701 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
702 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700703
704 gd->major = XENVBD_MAJOR;
705 gd->first_minor = minor;
706 gd->fops = &xlvbd_block_fops;
707 gd->private_data = info;
708 gd->driverfs_dev = &(info->xbdev->dev);
709 set_capacity(gd, capacity);
710
711 if (xlvbd_init_blk_queue(gd, sector_size)) {
712 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200713 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700714 }
715
716 info->rq = gd->queue;
717 info->gd = gd;
718
Tejun Heo4913efe2010-09-03 11:56:16 +0200719 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700720
721 if (vdisk_info & VDISK_READONLY)
722 set_disk_ro(gd, 1);
723
724 if (vdisk_info & VDISK_REMOVABLE)
725 gd->flags |= GENHD_FL_REMOVABLE;
726
727 if (vdisk_info & VDISK_CDROM)
728 gd->flags |= GENHD_FL_CD;
729
730 return 0;
731
Jan Beulich0e345822010-08-07 18:28:55 +0200732 release:
733 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700734 out:
735 return err;
736}
737
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200738static void xlvbd_release_gendisk(struct blkfront_info *info)
739{
740 unsigned int minor, nr_minors;
741 unsigned long flags;
742
743 if (info->rq == NULL)
744 return;
745
Steven Noonan34678112012-02-17 12:04:44 -0800746 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200747
748 /* No more blkif_request(). */
749 blk_stop_queue(info->rq);
750
751 /* No more gnttab callback work. */
752 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800753 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200754
755 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700756 flush_work(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200757
758 del_gendisk(info->gd);
759
760 minor = info->gd->first_minor;
761 nr_minors = info->gd->minors;
762 xlbd_release_minors(minor, nr_minors);
763
764 blk_cleanup_queue(info->rq);
765 info->rq = NULL;
766
767 put_disk(info->gd);
768 info->gd = NULL;
769}
770
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700771static void kick_pending_request_queues(struct blkfront_info *info)
772{
773 if (!RING_FULL(&info->ring)) {
774 /* Re-enable calldowns. */
775 blk_start_queue(info->rq);
776 /* Kick things off immediately. */
777 do_blkif_request(info->rq);
778 }
779}
780
781static void blkif_restart_queue(struct work_struct *work)
782{
783 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
784
Steven Noonan34678112012-02-17 12:04:44 -0800785 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700786 if (info->connected == BLKIF_STATE_CONNECTED)
787 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800788 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700789}
790
791static void blkif_free(struct blkfront_info *info, int suspend)
792{
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200793 struct llist_node *all_gnts;
Konrad Rzeszutek Wilkf84adf42013-02-13 13:01:55 -0500794 struct grant *persistent_gnt, *tmp;
Roger Pau Monneebb351c2012-12-04 15:21:53 +0100795 struct llist_node *n;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200796
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700797 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800798 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700799 info->connected = suspend ?
800 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
801 /* No more blkif_request(). */
802 if (info->rq)
803 blk_stop_queue(info->rq);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200804
805 /* Remove all persistent grants */
806 if (info->persistent_gnts_c) {
807 all_gnts = llist_del_all(&info->persistent_gnts);
Konrad Rzeszutek Wilkf84adf42013-02-13 13:01:55 -0500808 persistent_gnt = llist_entry(all_gnts, typeof(*(persistent_gnt)), node);
809 while (persistent_gnt) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200810 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monne07c540a2012-11-16 19:26:47 +0100811 __free_page(pfn_to_page(persistent_gnt->pfn));
Konrad Rzeszutek Wilkf84adf42013-02-13 13:01:55 -0500812 tmp = persistent_gnt;
813 n = persistent_gnt->node.next;
814 if (n)
815 persistent_gnt = llist_entry(n, typeof(*(persistent_gnt)), node);
816 else
817 persistent_gnt = NULL;
818 kfree(tmp);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200819 }
820 info->persistent_gnts_c = 0;
821 }
822
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700823 /* No more gnttab callback work. */
824 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800825 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700826
827 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700828 flush_work(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700829
830 /* Free resources associated with old device channel. */
831 if (info->ring_ref != GRANT_INVALID_REF) {
832 gnttab_end_foreign_access(info->ring_ref, 0,
833 (unsigned long)info->ring.sring);
834 info->ring_ref = GRANT_INVALID_REF;
835 info->ring.sring = NULL;
836 }
837 if (info->irq)
838 unbind_from_irqhandler(info->irq, info);
839 info->evtchn = info->irq = 0;
840
841}
842
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200843static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
844 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700845{
Roger Pau Monned62f6912012-12-07 19:00:31 +0100846 int i = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200847 struct bio_vec *bvec;
848 struct req_iterator iter;
849 unsigned long flags;
850 char *bvec_data;
851 void *shared_data;
852 unsigned int offset = 0;
853
854 if (bret->operation == BLKIF_OP_READ) {
855 /*
856 * Copy the data received from the backend into the bvec.
857 * Since bv_offset can be different than 0, and bv_len different
858 * than PAGE_SIZE, we have to keep track of the current offset,
859 * to be sure we are copying the data from the right shared page.
860 */
861 rq_for_each_segment(bvec, s->request, iter) {
862 BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
Roger Pau Monned62f6912012-12-07 19:00:31 +0100863 if (bvec->bv_offset < offset)
864 i++;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100865 BUG_ON(i >= s->req.u.rw.nr_segments);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200866 shared_data = kmap_atomic(
867 pfn_to_page(s->grants_used[i]->pfn));
868 bvec_data = bvec_kmap_irq(bvec, &flags);
869 memcpy(bvec_data, shared_data + bvec->bv_offset,
870 bvec->bv_len);
871 bvec_kunmap_irq(bvec_data, &flags);
872 kunmap_atomic(shared_data);
Roger Pau Monned62f6912012-12-07 19:00:31 +0100873 offset = bvec->bv_offset + bvec->bv_len;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200874 }
875 }
876 /* Add the persistent grant into the list of free grants */
877 for (i = 0; i < s->req.u.rw.nr_segments; i++) {
878 llist_add(&s->grants_used[i]->node, &info->persistent_gnts);
879 info->persistent_gnts_c++;
880 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700881}
882
883static irqreturn_t blkif_interrupt(int irq, void *dev_id)
884{
885 struct request *req;
886 struct blkif_response *bret;
887 RING_IDX i, rp;
888 unsigned long flags;
889 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500890 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700891
Steven Noonan34678112012-02-17 12:04:44 -0800892 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700893
894 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -0800895 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700896 return IRQ_HANDLED;
897 }
898
899 again:
900 rp = info->ring.sring->rsp_prod;
901 rmb(); /* Ensure we see queued responses up to 'rp'. */
902
903 for (i = info->ring.rsp_cons; i != rp; i++) {
904 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700905
906 bret = RING_GET_RESPONSE(&info->ring, i);
907 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400908 /*
909 * The backend has messed up and given us an id that we would
910 * never have given to it (we stamp it up to BLK_RING_SIZE -
911 * look in get_id_from_freelist.
912 */
913 if (id >= BLK_RING_SIZE) {
914 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
915 info->gd->disk_name, op_name(bret->operation), id);
916 /* We can't safely get the 'struct request' as
917 * the id is busted. */
918 continue;
919 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400920 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700921
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400922 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200923 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700924
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400925 if (add_id_to_freelist(info, id)) {
926 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
927 info->gd->disk_name, op_name(bret->operation), id);
928 continue;
929 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700930
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500931 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700932 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800933 case BLKIF_OP_DISCARD:
934 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
935 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400936 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
937 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +0800938 error = -EOPNOTSUPP;
939 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400940 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800941 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400942 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800943 }
944 __blk_end_request_all(req, error);
945 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400946 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700947 case BLKIF_OP_WRITE_BARRIER:
948 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400949 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
950 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500951 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400952 }
953 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400954 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400955 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
956 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400957 error = -EOPNOTSUPP;
958 }
959 if (unlikely(error)) {
960 if (error == -EOPNOTSUPP)
961 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200962 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400963 info->flush_op = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200964 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700965 }
966 /* fall through */
967 case BLKIF_OP_READ:
968 case BLKIF_OP_WRITE:
969 if (unlikely(bret->status != BLKIF_RSP_OKAY))
970 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
971 "request: %x\n", bret->status);
972
Tejun Heo40cbbb72009-04-23 11:05:19 +0900973 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700974 break;
975 default:
976 BUG();
977 }
978 }
979
980 info->ring.rsp_cons = i;
981
982 if (i != info->ring.req_prod_pvt) {
983 int more_to_do;
984 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
985 if (more_to_do)
986 goto again;
987 } else
988 info->ring.sring->rsp_event = i + 1;
989
990 kick_pending_request_queues(info);
991
Steven Noonan34678112012-02-17 12:04:44 -0800992 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700993
994 return IRQ_HANDLED;
995}
996
997
998static int setup_blkring(struct xenbus_device *dev,
999 struct blkfront_info *info)
1000{
1001 struct blkif_sring *sring;
1002 int err;
1003
1004 info->ring_ref = GRANT_INVALID_REF;
1005
Ian Campbella144ff02008-06-17 10:47:08 +02001006 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001007 if (!sring) {
1008 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1009 return -ENOMEM;
1010 }
1011 SHARED_RING_INIT(sring);
1012 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1013
Jens Axboe9e973e62009-02-24 08:10:09 +01001014 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
1015
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001016 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
1017 if (err < 0) {
1018 free_page((unsigned long)sring);
1019 info->ring.sring = NULL;
1020 goto fail;
1021 }
1022 info->ring_ref = err;
1023
1024 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1025 if (err)
1026 goto fail;
1027
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001028 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1029 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001030 if (err <= 0) {
1031 xenbus_dev_fatal(dev, err,
1032 "bind_evtchn_to_irqhandler failed");
1033 goto fail;
1034 }
1035 info->irq = err;
1036
1037 return 0;
1038fail:
1039 blkif_free(info, 0);
1040 return err;
1041}
1042
1043
1044/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001045static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001046 struct blkfront_info *info)
1047{
1048 const char *message = NULL;
1049 struct xenbus_transaction xbt;
1050 int err;
1051
1052 /* Create shared ring, alloc event channel. */
1053 err = setup_blkring(dev, info);
1054 if (err)
1055 goto out;
1056
1057again:
1058 err = xenbus_transaction_start(&xbt);
1059 if (err) {
1060 xenbus_dev_fatal(dev, err, "starting transaction");
1061 goto destroy_blkring;
1062 }
1063
1064 err = xenbus_printf(xbt, dev->nodename,
1065 "ring-ref", "%u", info->ring_ref);
1066 if (err) {
1067 message = "writing ring-ref";
1068 goto abort_transaction;
1069 }
1070 err = xenbus_printf(xbt, dev->nodename,
1071 "event-channel", "%u", info->evtchn);
1072 if (err) {
1073 message = "writing event-channel";
1074 goto abort_transaction;
1075 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001076 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1077 XEN_IO_PROTO_ABI_NATIVE);
1078 if (err) {
1079 message = "writing protocol";
1080 goto abort_transaction;
1081 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001082 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001083 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001084 if (err)
1085 dev_warn(&dev->dev,
1086 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001087
1088 err = xenbus_transaction_end(xbt, 0);
1089 if (err) {
1090 if (err == -EAGAIN)
1091 goto again;
1092 xenbus_dev_fatal(dev, err, "completing transaction");
1093 goto destroy_blkring;
1094 }
1095
1096 xenbus_switch_state(dev, XenbusStateInitialised);
1097
1098 return 0;
1099
1100 abort_transaction:
1101 xenbus_transaction_end(xbt, 1);
1102 if (message)
1103 xenbus_dev_fatal(dev, err, "%s", message);
1104 destroy_blkring:
1105 blkif_free(info, 0);
1106 out:
1107 return err;
1108}
1109
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001110/**
1111 * Entry point to this code when a new device is created. Allocate the basic
1112 * structures and the ring buffer for communication with the backend, and
1113 * inform the backend of the appropriate details for those. Switch to
1114 * Initialised state.
1115 */
1116static int blkfront_probe(struct xenbus_device *dev,
1117 const struct xenbus_device_id *id)
1118{
1119 int err, vdevice, i;
1120 struct blkfront_info *info;
1121
1122 /* FIXME: Use dynamic device id if this is not set. */
1123 err = xenbus_scanf(XBT_NIL, dev->nodename,
1124 "virtual-device", "%i", &vdevice);
1125 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001126 /* go looking in the extended area instead */
1127 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1128 "%i", &vdevice);
1129 if (err != 1) {
1130 xenbus_dev_fatal(dev, err, "reading virtual-device");
1131 return err;
1132 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001133 }
1134
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001135 if (xen_hvm_domain()) {
1136 char *type;
1137 int len;
1138 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +01001139 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001140 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001141
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001142 if (!VDEV_IS_EXTENDED(vdevice))
1143 major = BLKIF_MAJOR(vdevice);
1144 else
1145 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001146
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001147 if (major != XENVBD_MAJOR) {
1148 printk(KERN_INFO
1149 "%s: HVM does not support vbd %d as xen block device\n",
1150 __FUNCTION__, vdevice);
1151 return -ENODEV;
1152 }
1153 }
1154 /* do not create a PV cdrom device if we are an HVM guest */
1155 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1156 if (IS_ERR(type))
1157 return -ENODEV;
1158 if (strncmp(type, "cdrom", 5) == 0) {
1159 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001160 return -ENODEV;
1161 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001162 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001163 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001164 info = kzalloc(sizeof(*info), GFP_KERNEL);
1165 if (!info) {
1166 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1167 return -ENOMEM;
1168 }
1169
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001170 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001171 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001172 info->xbdev = dev;
1173 info->vdevice = vdevice;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001174 init_llist_head(&info->persistent_gnts);
1175 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001176 info->connected = BLKIF_STATE_DISCONNECTED;
1177 INIT_WORK(&info->work, blkif_restart_queue);
1178
1179 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001180 info->shadow[i].req.u.rw.id = i+1;
1181 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001182
1183 /* Front end dir is a number, which is used as the id. */
1184 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001185 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001186
Ian Campbell203fd612009-12-04 15:33:54 +00001187 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001188 if (err) {
1189 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001190 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001191 return err;
1192 }
1193
1194 return 0;
1195}
1196
1197
1198static int blkif_recover(struct blkfront_info *info)
1199{
1200 int i;
1201 struct blkif_request *req;
1202 struct blk_shadow *copy;
1203 int j;
1204
1205 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +02001206 copy = kmalloc(sizeof(info->shadow),
1207 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001208 if (!copy)
1209 return -ENOMEM;
1210 memcpy(copy, info->shadow, sizeof(info->shadow));
1211
1212 /* Stage 2: Set up free list. */
1213 memset(&info->shadow, 0, sizeof(info->shadow));
1214 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001215 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001216 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001217 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001218
1219 /* Stage 3: Find pending requests and requeue them. */
1220 for (i = 0; i < BLK_RING_SIZE; i++) {
1221 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001222 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001223 continue;
1224
1225 /* Grab a request slot and copy shadow state into it. */
1226 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
1227 *req = copy[i].req;
1228
1229 /* We get a new request id, and must reset the shadow state. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001230 req->u.rw.id = get_id_from_freelist(info);
1231 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001232
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001233 if (req->operation != BLKIF_OP_DISCARD) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001234 /* Rewrite any grant references invalidated by susp/resume. */
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001235 for (j = 0; j < req->u.rw.nr_segments; j++)
1236 gnttab_grant_foreign_access_ref(
1237 req->u.rw.seg[j].gref,
1238 info->xbdev->otherend_id,
1239 pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001240 0);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001241 }
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001242 info->shadow[req->u.rw.id].req = *req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001243
1244 info->ring.req_prod_pvt++;
1245 }
1246
1247 kfree(copy);
1248
1249 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1250
Steven Noonan34678112012-02-17 12:04:44 -08001251 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001252
1253 /* Now safe for us to use the shared ring */
1254 info->connected = BLKIF_STATE_CONNECTED;
1255
1256 /* Send off requeued requests */
1257 flush_requests(info);
1258
1259 /* Kick any other new requests queued since we resumed */
1260 kick_pending_request_queues(info);
1261
Steven Noonan34678112012-02-17 12:04:44 -08001262 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001263
1264 return 0;
1265}
1266
1267/**
1268 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1269 * driver restart. We tear down our blkif structure and recreate it, but
1270 * leave the device-layer structures intact so that this is transparent to the
1271 * rest of the kernel.
1272 */
1273static int blkfront_resume(struct xenbus_device *dev)
1274{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001275 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001276 int err;
1277
1278 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1279
1280 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1281
Ian Campbell203fd612009-12-04 15:33:54 +00001282 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001283 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
1284 err = blkif_recover(info);
1285
1286 return err;
1287}
1288
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001289static void
1290blkfront_closing(struct blkfront_info *info)
1291{
1292 struct xenbus_device *xbdev = info->xbdev;
1293 struct block_device *bdev = NULL;
1294
1295 mutex_lock(&info->mutex);
1296
1297 if (xbdev->state == XenbusStateClosing) {
1298 mutex_unlock(&info->mutex);
1299 return;
1300 }
1301
1302 if (info->gd)
1303 bdev = bdget_disk(info->gd, 0);
1304
1305 mutex_unlock(&info->mutex);
1306
1307 if (!bdev) {
1308 xenbus_frontend_closed(xbdev);
1309 return;
1310 }
1311
1312 mutex_lock(&bdev->bd_mutex);
1313
Daniel Stodden7b32d102010-04-30 22:01:23 +00001314 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001315 xenbus_dev_error(xbdev, -EBUSY,
1316 "Device in use; refusing to close");
1317 xenbus_switch_state(xbdev, XenbusStateClosing);
1318 } else {
1319 xlvbd_release_gendisk(info);
1320 xenbus_frontend_closed(xbdev);
1321 }
1322
1323 mutex_unlock(&bdev->bd_mutex);
1324 bdput(bdev);
1325}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001326
Li Dongyanged30bf32011-09-01 18:39:09 +08001327static void blkfront_setup_discard(struct blkfront_info *info)
1328{
1329 int err;
1330 char *type;
1331 unsigned int discard_granularity;
1332 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001333 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001334
1335 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1336 if (IS_ERR(type))
1337 return;
1338
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001339 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001340 if (strncmp(type, "phy", 3) == 0) {
1341 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1342 "discard-granularity", "%u", &discard_granularity,
1343 "discard-alignment", "%u", &discard_alignment,
1344 NULL);
1345 if (!err) {
1346 info->feature_discard = 1;
1347 info->discard_granularity = discard_granularity;
1348 info->discard_alignment = discard_alignment;
1349 }
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001350 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1351 "discard-secure", "%d", &discard_secure,
1352 NULL);
1353 if (!err)
1354 info->feature_secdiscard = discard_secure;
1355
Li Dongyanged30bf32011-09-01 18:39:09 +08001356 } else if (strncmp(type, "file", 4) == 0)
1357 info->feature_discard = 1;
1358
1359 kfree(type);
1360}
1361
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001362/*
1363 * Invoked when the backend is finally 'ready' (and has told produced
1364 * the details about the physical device - #sectors, size, etc).
1365 */
1366static void blkfront_connect(struct blkfront_info *info)
1367{
1368 unsigned long long sectors;
1369 unsigned long sector_size;
1370 unsigned int binfo;
1371 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001372 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001373
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001374 switch (info->connected) {
1375 case BLKIF_STATE_CONNECTED:
1376 /*
1377 * Potentially, the back-end may be signalling
1378 * a capacity change; update the capacity.
1379 */
1380 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1381 "sectors", "%Lu", &sectors);
1382 if (XENBUS_EXIST_ERR(err))
1383 return;
1384 printk(KERN_INFO "Setting capacity to %Lu\n",
1385 sectors);
1386 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001387 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001388
1389 /* fall through */
1390 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001391 return;
1392
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001393 default:
1394 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001395 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001396
1397 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1398 __func__, info->xbdev->otherend);
1399
1400 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1401 "sectors", "%llu", &sectors,
1402 "info", "%u", &binfo,
1403 "sector-size", "%lu", &sector_size,
1404 NULL);
1405 if (err) {
1406 xenbus_dev_fatal(info->xbdev, err,
1407 "reading backend fields at %s",
1408 info->xbdev->otherend);
1409 return;
1410 }
1411
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001412 info->feature_flush = 0;
1413 info->flush_op = 0;
1414
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001415 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001416 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001417 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001418
1419 /*
1420 * If there's no "feature-barrier" defined, then it means
1421 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001422 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001423 *
Tejun Heo6958f142010-09-03 11:56:16 +02001424 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001425 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001426 if (!err && barrier) {
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001427 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001428 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1429 }
1430 /*
1431 * And if there is "feature-flush-cache" use that above
1432 * barriers.
1433 */
1434 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1435 "feature-flush-cache", "%d", &flush,
1436 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001437
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001438 if (!err && flush) {
1439 info->feature_flush = REQ_FLUSH;
1440 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1441 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001442
1443 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1444 "feature-discard", "%d", &discard,
1445 NULL);
1446
1447 if (!err && discard)
1448 blkfront_setup_discard(info);
1449
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001450 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1451 "feature-persistent", "%u", &persistent,
1452 NULL);
1453 if (err)
1454 info->feature_persistent = 0;
1455 else
1456 info->feature_persistent = persistent;
1457
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001458 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001459 if (err) {
1460 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1461 info->xbdev->otherend);
1462 return;
1463 }
1464
1465 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1466
1467 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001468 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001469 info->connected = BLKIF_STATE_CONNECTED;
1470 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001471 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001472
1473 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001474
1475 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001476}
1477
1478/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001479 * Callback received when the backend's state changes.
1480 */
Ian Campbell203fd612009-12-04 15:33:54 +00001481static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001482 enum xenbus_state backend_state)
1483{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001484 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001485
Ian Campbell203fd612009-12-04 15:33:54 +00001486 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001487
1488 switch (backend_state) {
1489 case XenbusStateInitialising:
1490 case XenbusStateInitWait:
1491 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001492 case XenbusStateReconfiguring:
1493 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001494 case XenbusStateUnknown:
1495 case XenbusStateClosed:
1496 break;
1497
1498 case XenbusStateConnected:
1499 blkfront_connect(info);
1500 break;
1501
1502 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001503 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001504 break;
1505 }
1506}
1507
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001508static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001509{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001510 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1511 struct block_device *bdev = NULL;
1512 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001513
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001514 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001515
1516 blkif_free(info, 0);
1517
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001518 mutex_lock(&info->mutex);
1519
1520 disk = info->gd;
1521 if (disk)
1522 bdev = bdget_disk(disk, 0);
1523
1524 info->xbdev = NULL;
1525 mutex_unlock(&info->mutex);
1526
1527 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001528 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001529 return 0;
1530 }
1531
1532 /*
1533 * The xbdev was removed before we reached the Closed
1534 * state. See if it's safe to remove the disk. If the bdev
1535 * isn't closed yet, we let release take care of it.
1536 */
1537
1538 mutex_lock(&bdev->bd_mutex);
1539 info = disk->private_data;
1540
Daniel Stoddend54142c2010-08-07 18:51:21 +02001541 dev_warn(disk_to_dev(disk),
1542 "%s was hot-unplugged, %d stale handles\n",
1543 xbdev->nodename, bdev->bd_openers);
1544
Daniel Stodden7b32d102010-04-30 22:01:23 +00001545 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001546 xlvbd_release_gendisk(info);
1547 disk->private_data = NULL;
1548 kfree(info);
1549 }
1550
1551 mutex_unlock(&bdev->bd_mutex);
1552 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001553
1554 return 0;
1555}
1556
Christian Limpach1d78d702008-04-02 10:54:04 -07001557static int blkfront_is_ready(struct xenbus_device *dev)
1558{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001559 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001560
Jan Beulich5d7ed202010-08-07 18:31:12 +02001561 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001562}
1563
Al Viroa63c8482008-03-02 10:23:47 -05001564static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001565{
Daniel Stodden13961742010-08-07 18:36:53 +02001566 struct gendisk *disk = bdev->bd_disk;
1567 struct blkfront_info *info;
1568 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001569
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001570 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001571
Daniel Stodden13961742010-08-07 18:36:53 +02001572 info = disk->private_data;
1573 if (!info) {
1574 /* xbdev gone */
1575 err = -ERESTARTSYS;
1576 goto out;
1577 }
1578
1579 mutex_lock(&info->mutex);
1580
1581 if (!info->gd)
1582 /* xbdev is closed */
1583 err = -ERESTARTSYS;
1584
1585 mutex_unlock(&info->mutex);
1586
Daniel Stodden13961742010-08-07 18:36:53 +02001587out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001588 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001589 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001590}
1591
Al Viroa63c8482008-03-02 10:23:47 -05001592static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001593{
Al Viroa63c8482008-03-02 10:23:47 -05001594 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001595 struct block_device *bdev;
1596 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001597
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001598 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001599
1600 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001601
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001602 if (bdev->bd_openers)
1603 goto out;
1604
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001605 /*
1606 * Check if we have been instructed to close. We will have
1607 * deferred this request, because the bdev was still open.
1608 */
1609
1610 mutex_lock(&info->mutex);
1611 xbdev = info->xbdev;
1612
1613 if (xbdev && xbdev->state == XenbusStateClosing) {
1614 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001615 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001616 xlvbd_release_gendisk(info);
1617 xenbus_frontend_closed(info->xbdev);
1618 }
1619
1620 mutex_unlock(&info->mutex);
1621
1622 if (!xbdev) {
1623 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001624 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001625 xlvbd_release_gendisk(info);
1626 disk->private_data = NULL;
1627 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001628 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001629
Jens Axboea4cc14e2010-08-08 21:50:05 -04001630out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01001631 bdput(bdev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001632 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001633 return 0;
1634}
1635
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001636static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001637{
1638 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001639 .open = blkif_open,
1640 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001641 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001642 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001643};
1644
1645
Márton Némethec9c42e2010-01-10 13:39:52 +01001646static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001647 { "vbd" },
1648 { "" }
1649};
1650
Jan Beulich73db1442011-12-22 09:08:13 +00001651static DEFINE_XENBUS_DRIVER(blkfront, ,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001652 .probe = blkfront_probe,
1653 .remove = blkfront_remove,
1654 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001655 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001656 .is_ready = blkfront_is_ready,
Jan Beulich73db1442011-12-22 09:08:13 +00001657);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001658
1659static int __init xlblk_init(void)
1660{
Laszlo Ersek469738e2011-10-07 21:34:38 +02001661 int ret;
1662
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001663 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001664 return -ENODEV;
1665
Igor Mammedove95ae5a2012-03-27 19:31:08 +02001666 if (xen_hvm_domain() && !xen_platform_pci_unplug)
Igor Mammedovb9136d22012-03-21 15:08:38 +01001667 return -ENODEV;
1668
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001669 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1670 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1671 XENVBD_MAJOR, DEV_NAME);
1672 return -ENODEV;
1673 }
1674
Jan Beulich73db1442011-12-22 09:08:13 +00001675 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02001676 if (ret) {
1677 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1678 return ret;
1679 }
1680
1681 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001682}
1683module_init(xlblk_init);
1684
1685
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001686static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001687{
Jan Beulich86050672012-04-05 16:04:52 +01001688 xenbus_unregister_driver(&blkfront_driver);
1689 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1690 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001691}
1692module_exit(xlblk_exit);
1693
1694MODULE_DESCRIPTION("Xen virtual block device frontend");
1695MODULE_LICENSE("GPL");
1696MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001697MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001698MODULE_ALIAS("xenblk");