blob: bac8cf31319b6b269840bd03583679c0cb4aec78 [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 Monne155b7ed2013-03-18 17:49:34 +010047#include <linux/list.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;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010071 struct list_head node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020072};
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;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020077 struct grant **grants_used;
78 struct grant **indirect_grants;
Roger Pau Monneb7649152013-05-02 10:58:50 +020079 struct scatterlist *sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020080};
81
82struct split_bio {
83 struct bio *bio;
84 atomic_t pending;
85 int err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070086};
87
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020088static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070089static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070090
Roger Pau Monne402b27f2013-04-18 16:06:54 +020091/*
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
95 */
96
97static unsigned int xen_blkif_max_segments = 32;
98
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080099#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700100
101/*
102 * We have one of these per vbd, whether ide, scsi or 'other'. They
103 * hang in private_data off the gendisk structure. We may end up
104 * putting all kinds of interesting stuff here :-)
105 */
106struct blkfront_info
107{
Steven Noonan34678112012-02-17 12:04:44 -0800108 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000109 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700110 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700111 struct gendisk *gd;
112 int vdevice;
113 blkif_vdev_t handle;
114 enum blkif_state connected;
115 int ring_ref;
116 struct blkif_front_ring ring;
117 unsigned int evtchn, irq;
118 struct request_queue *rq;
119 struct work_struct work;
120 struct gnttab_free_callback callback;
121 struct blk_shadow shadow[BLK_RING_SIZE];
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100122 struct list_head persistent_gnts;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200123 unsigned int persistent_gnts_c;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700124 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200125 unsigned int feature_flush;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400126 unsigned int flush_op;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400127 unsigned int feature_discard:1;
128 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800129 unsigned int discard_granularity;
130 unsigned int discard_alignment;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200131 unsigned int feature_persistent:1;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200132 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700133 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700134};
135
Jan Beulich0e345822010-08-07 18:28:55 +0200136static unsigned int nr_minors;
137static unsigned long *minors;
138static DEFINE_SPINLOCK(minor_lock);
139
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700140#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
141 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
142#define GRANT_INVALID_REF 0
143
144#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700145#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700146
147#define BLKIF_MAJOR(dev) ((dev)>>8)
148#define BLKIF_MINOR(dev) ((dev) & 0xff)
149
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700150#define EXT_SHIFT 28
151#define EXTENDED (1<<EXT_SHIFT)
152#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
153#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000154#define EMULATED_HD_DISK_MINOR_OFFSET (0)
155#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200156#define EMULATED_SD_DISK_MINOR_OFFSET (0)
157#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700158
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700159#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700160
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200161#define SEGS_PER_INDIRECT_FRAME \
162 (PAGE_SIZE/sizeof(struct blkif_request_segment_aligned))
163#define INDIRECT_GREFS(_segs) \
164 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
165
166static int blkfront_setup_indirect(struct blkfront_info *info);
167
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700168static int get_id_from_freelist(struct blkfront_info *info)
169{
170 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200171 BUG_ON(free >= BLK_RING_SIZE);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400172 info->shadow_free = info->shadow[free].req.u.rw.id;
173 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700174 return free;
175}
176
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400177static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700178 unsigned long id)
179{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400180 if (info->shadow[id].req.u.rw.id != id)
181 return -EINVAL;
182 if (info->shadow[id].request == NULL)
183 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400184 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400185 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700186 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400187 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700188}
189
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100190static int fill_grant_buffer(struct blkfront_info *info, int num)
191{
192 struct page *granted_page;
193 struct grant *gnt_list_entry, *n;
194 int i = 0;
195
196 while(i < num) {
197 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
198 if (!gnt_list_entry)
199 goto out_of_memory;
200
201 granted_page = alloc_page(GFP_NOIO);
202 if (!granted_page) {
203 kfree(gnt_list_entry);
204 goto out_of_memory;
205 }
206
207 gnt_list_entry->pfn = page_to_pfn(granted_page);
208 gnt_list_entry->gref = GRANT_INVALID_REF;
209 list_add(&gnt_list_entry->node, &info->persistent_gnts);
210 i++;
211 }
212
213 return 0;
214
215out_of_memory:
216 list_for_each_entry_safe(gnt_list_entry, n,
217 &info->persistent_gnts, node) {
218 list_del(&gnt_list_entry->node);
219 __free_page(pfn_to_page(gnt_list_entry->pfn));
220 kfree(gnt_list_entry);
221 i--;
222 }
223 BUG_ON(i != 0);
224 return -ENOMEM;
225}
226
227static struct grant *get_grant(grant_ref_t *gref_head,
228 struct blkfront_info *info)
229{
230 struct grant *gnt_list_entry;
231 unsigned long buffer_mfn;
232
233 BUG_ON(list_empty(&info->persistent_gnts));
234 gnt_list_entry = list_first_entry(&info->persistent_gnts, struct grant,
235 node);
236 list_del(&gnt_list_entry->node);
237
238 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
239 info->persistent_gnts_c--;
240 return gnt_list_entry;
241 }
242
243 /* Assign a gref to this page */
244 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
245 BUG_ON(gnt_list_entry->gref == -ENOSPC);
246 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
247 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
248 info->xbdev->otherend_id,
249 buffer_mfn, 0);
250 return gnt_list_entry;
251}
252
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400253static const char *op_name(int op)
254{
255 static const char *const names[] = {
256 [BLKIF_OP_READ] = "read",
257 [BLKIF_OP_WRITE] = "write",
258 [BLKIF_OP_WRITE_BARRIER] = "barrier",
259 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
260 [BLKIF_OP_DISCARD] = "discard" };
261
262 if (op < 0 || op >= ARRAY_SIZE(names))
263 return "unknown";
264
265 if (!names[op])
266 return "reserved";
267
268 return names[op];
269}
Jan Beulich0e345822010-08-07 18:28:55 +0200270static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
271{
272 unsigned int end = minor + nr;
273 int rc;
274
275 if (end > nr_minors) {
276 unsigned long *bitmap, *old;
277
Thomas Meyerf0941482011-11-29 22:08:00 +0100278 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200279 GFP_KERNEL);
280 if (bitmap == NULL)
281 return -ENOMEM;
282
283 spin_lock(&minor_lock);
284 if (end > nr_minors) {
285 old = minors;
286 memcpy(bitmap, minors,
287 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
288 minors = bitmap;
289 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
290 } else
291 old = bitmap;
292 spin_unlock(&minor_lock);
293 kfree(old);
294 }
295
296 spin_lock(&minor_lock);
297 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900298 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200299 rc = 0;
300 } else
301 rc = -EBUSY;
302 spin_unlock(&minor_lock);
303
304 return rc;
305}
306
307static void xlbd_release_minors(unsigned int minor, unsigned int nr)
308{
309 unsigned int end = minor + nr;
310
311 BUG_ON(end > nr_minors);
312 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900313 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200314 spin_unlock(&minor_lock);
315}
316
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700317static void blkif_restart_queue_callback(void *arg)
318{
319 struct blkfront_info *info = (struct blkfront_info *)arg;
320 schedule_work(&info->work);
321}
322
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700323static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800324{
325 /* We don't have real geometry info, but let's at least return
326 values consistent with the size of the device */
327 sector_t nsect = get_capacity(bd->bd_disk);
328 sector_t cylinders = nsect;
329
330 hg->heads = 0xff;
331 hg->sectors = 0x3f;
332 sector_div(cylinders, hg->heads * hg->sectors);
333 hg->cylinders = cylinders;
334 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
335 hg->cylinders = 0xffff;
336 return 0;
337}
338
Al Viroa63c8482008-03-02 10:23:47 -0500339static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200340 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200341{
Al Viroa63c8482008-03-02 10:23:47 -0500342 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200343 int i;
344
345 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
346 command, (long)argument);
347
348 switch (command) {
349 case CDROMMULTISESSION:
350 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
351 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
352 if (put_user(0, (char __user *)(argument + i)))
353 return -EFAULT;
354 return 0;
355
356 case CDROM_GET_CAPABILITY: {
357 struct gendisk *gd = info->gd;
358 if (gd->flags & GENHD_FL_CD)
359 return 0;
360 return -EINVAL;
361 }
362
363 default:
364 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
365 command);*/
366 return -EINVAL; /* same return as native Linux */
367 }
368
369 return 0;
370}
371
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400373 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400374 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700375 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400376 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700377 */
378static int blkif_queue_request(struct request *req)
379{
380 struct blkfront_info *info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700381 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700382 unsigned long id;
383 unsigned int fsect, lsect;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200384 int i, ref, n;
385 struct blkif_request_segment_aligned *segments = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200386
387 /*
388 * Used to store if we are able to queue the request by just using
389 * existing persistent grants, or if we have to get new grants,
390 * as there are not sufficiently many free.
391 */
392 bool new_persistent_gnts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700393 grant_ref_t gref_head;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200394 struct grant *gnt_list_entry = NULL;
Jens Axboe9e973e62009-02-24 08:10:09 +0100395 struct scatterlist *sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200396 int nseg, max_grefs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700397
398 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
399 return 1;
400
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200401 max_grefs = info->max_indirect_segments ?
402 info->max_indirect_segments +
403 INDIRECT_GREFS(info->max_indirect_segments) :
404 BLKIF_MAX_SEGMENTS_PER_REQUEST;
405
406 /* Check if we have enough grants to allocate a requests */
407 if (info->persistent_gnts_c < max_grefs) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200408 new_persistent_gnts = 1;
409 if (gnttab_alloc_grant_references(
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200410 max_grefs - info->persistent_gnts_c,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200411 &gref_head) < 0) {
412 gnttab_request_free_callback(
413 &info->callback,
414 blkif_restart_queue_callback,
415 info,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200416 max_grefs);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200417 return 1;
418 }
419 } else
420 new_persistent_gnts = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700421
422 /* Fill out a communications ring structure. */
423 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
424 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400425 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700426
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400427 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800428 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800429 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200430 ring_req->u.discard.id = id;
431 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400432 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
433 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
434 else
435 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800436 } else {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200437 BUG_ON(info->max_indirect_segments == 0 &&
438 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
439 BUG_ON(info->max_indirect_segments &&
440 req->nr_phys_segments > info->max_indirect_segments);
Roger Pau Monneb7649152013-05-02 10:58:50 +0200441 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200442 ring_req->u.rw.id = id;
443 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
444 /*
445 * The indirect operation can only be a BLKIF_OP_READ or
446 * BLKIF_OP_WRITE
447 */
448 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
449 ring_req->operation = BLKIF_OP_INDIRECT;
450 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
451 BLKIF_OP_WRITE : BLKIF_OP_READ;
452 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
453 ring_req->u.indirect.handle = info->handle;
454 ring_req->u.indirect.nr_segments = nseg;
455 } else {
456 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
457 ring_req->u.rw.handle = info->handle;
458 ring_req->operation = rq_data_dir(req) ?
459 BLKIF_OP_WRITE : BLKIF_OP_READ;
460 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
461 /*
462 * Ideally we can do an unordered flush-to-disk. In case the
463 * backend onlysupports barriers, use that. A barrier request
464 * a superset of FUA, so we can implement it the same
465 * way. (It's also a FLUSH+FUA, since it is
466 * guaranteed ordered WRT previous writes.)
467 */
468 ring_req->operation = info->flush_op;
469 }
470 ring_req->u.rw.nr_segments = nseg;
471 }
Roger Pau Monneb7649152013-05-02 10:58:50 +0200472 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800473 fsect = sg->offset >> 9;
474 lsect = fsect + (sg->length >> 9) - 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700475
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200476 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
477 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
478 if (segments)
479 kunmap_atomic(segments);
480
481 n = i / SEGS_PER_INDIRECT_FRAME;
482 gnt_list_entry = get_grant(&gref_head, info);
483 info->shadow[id].indirect_grants[n] = gnt_list_entry;
484 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
485 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
486 }
487
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100488 gnt_list_entry = get_grant(&gref_head, info);
489 ref = gnt_list_entry->gref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200490
491 info->shadow[id].grants_used[i] = gnt_list_entry;
492
493 if (rq_data_dir(req)) {
494 char *bvec_data;
495 void *shared_data;
496
497 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
498
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200499 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200500 bvec_data = kmap_atomic(sg_page(sg));
501
502 /*
503 * this does not wipe data stored outside the
504 * range sg->offset..sg->offset+sg->length.
505 * Therefore, blkback *could* see data from
506 * previous requests. This is OK as long as
507 * persistent grants are shared with just one
508 * domain. It may need refactoring if this
509 * changes
510 */
511 memcpy(shared_data + sg->offset,
512 bvec_data + sg->offset,
513 sg->length);
514
515 kunmap_atomic(bvec_data);
516 kunmap_atomic(shared_data);
517 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200518 if (ring_req->operation != BLKIF_OP_INDIRECT) {
519 ring_req->u.rw.seg[i] =
520 (struct blkif_request_segment) {
521 .gref = ref,
522 .first_sect = fsect,
523 .last_sect = lsect };
524 } else {
525 n = i % SEGS_PER_INDIRECT_FRAME;
526 segments[n] =
527 (struct blkif_request_segment_aligned) {
528 .gref = ref,
529 .first_sect = fsect,
530 .last_sect = lsect };
531 }
Li Dongyanged30bf32011-09-01 18:39:09 +0800532 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200533 if (segments)
534 kunmap_atomic(segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700535 }
536
537 info->ring.req_prod_pvt++;
538
539 /* Keep a private copy so we can reissue requests when recovering. */
540 info->shadow[id].req = *ring_req;
541
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200542 if (new_persistent_gnts)
543 gnttab_free_grant_references(gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700544
545 return 0;
546}
547
548
549static inline void flush_requests(struct blkfront_info *info)
550{
551 int notify;
552
553 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
554
555 if (notify)
556 notify_remote_via_irq(info->irq);
557}
558
559/*
560 * do_blkif_request
561 * read a block; request is in a request queue
562 */
Jens Axboe165125e2007-07-24 09:28:11 +0200563static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700564{
565 struct blkfront_info *info = NULL;
566 struct request *req;
567 int queued;
568
569 pr_debug("Entered do_blkif_request\n");
570
571 queued = 0;
572
Tejun Heo9934c8c2009-05-08 11:54:16 +0900573 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700574 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700575
576 if (RING_FULL(&info->ring))
577 goto wait;
578
Tejun Heo9934c8c2009-05-08 11:54:16 +0900579 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900580
Konrad Rzeszutek Wilkd11e6152011-09-16 15:15:14 -0400581 if ((req->cmd_type != REQ_TYPE_FS) ||
582 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
583 !info->flush_op)) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900584 __blk_end_request_all(req, -EIO);
585 continue;
586 }
587
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700588 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900589 "(%u/%u) buffer:%p [%s]\n",
590 req, req->cmd, (unsigned long)blk_rq_pos(req),
591 blk_rq_cur_sectors(req), blk_rq_sectors(req),
592 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700593
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700594 if (blkif_queue_request(req)) {
595 blk_requeue_request(rq, req);
596wait:
597 /* Avoid pointless unplugs. */
598 blk_stop_queue(rq);
599 break;
600 }
601
602 queued++;
603 }
604
605 if (queued != 0)
606 flush_requests(info);
607}
608
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200609static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
610 unsigned int segments)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700611{
Jens Axboe165125e2007-07-24 09:28:11 +0200612 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800613 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700614
Steven Noonan34678112012-02-17 12:04:44 -0800615 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700616 if (rq == NULL)
617 return -1;
618
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900619 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700620
Li Dongyanged30bf32011-09-01 18:39:09 +0800621 if (info->feature_discard) {
622 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
623 blk_queue_max_discard_sectors(rq, get_capacity(gd));
624 rq->limits.discard_granularity = info->discard_granularity;
625 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400626 if (info->feature_secdiscard)
627 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800628 }
629
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700630 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400631 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500632 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700633
634 /* Each segment in a request is up to an aligned page in size. */
635 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
636 blk_queue_max_segment_size(rq, PAGE_SIZE);
637
638 /* Ensure a merged request will fit in a single I/O ring slot. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200639 blk_queue_max_segments(rq, segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700640
641 /* Make sure buffer addresses are sector-aligned. */
642 blk_queue_dma_alignment(rq, 511);
643
Ian Campbell1c91fe12008-06-17 10:47:08 +0200644 /* Make sure we don't use bounce buffers. */
645 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
646
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700647 gd->queue = rq;
648
649 return 0;
650}
651
652
Tejun Heo4913efe2010-09-03 11:56:16 +0200653static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700654{
Tejun Heo4913efe2010-09-03 11:56:16 +0200655 blk_queue_flush(info->rq, info->feature_flush);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200656 printk(KERN_INFO "blkfront: %s: %s: %s %s %s %s %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200657 info->gd->disk_name,
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400658 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
659 "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
660 "flush diskcache" : "barrier or flush"),
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200661 info->feature_flush ? "enabled;" : "disabled;",
662 "persistent grants:",
663 info->feature_persistent ? "enabled;" : "disabled;",
664 "indirect descriptors:",
665 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700666}
667
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000668static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
669{
670 int major;
671 major = BLKIF_MAJOR(vdevice);
672 *minor = BLKIF_MINOR(vdevice);
673 switch (major) {
674 case XEN_IDE0_MAJOR:
675 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
676 *minor = ((*minor / 64) * PARTS_PER_DISK) +
677 EMULATED_HD_DISK_MINOR_OFFSET;
678 break;
679 case XEN_IDE1_MAJOR:
680 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
681 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
682 EMULATED_HD_DISK_MINOR_OFFSET;
683 break;
684 case XEN_SCSI_DISK0_MAJOR:
685 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
686 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
687 break;
688 case XEN_SCSI_DISK1_MAJOR:
689 case XEN_SCSI_DISK2_MAJOR:
690 case XEN_SCSI_DISK3_MAJOR:
691 case XEN_SCSI_DISK4_MAJOR:
692 case XEN_SCSI_DISK5_MAJOR:
693 case XEN_SCSI_DISK6_MAJOR:
694 case XEN_SCSI_DISK7_MAJOR:
695 *offset = (*minor / PARTS_PER_DISK) +
696 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
697 EMULATED_SD_DISK_NAME_OFFSET;
698 *minor = *minor +
699 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
700 EMULATED_SD_DISK_MINOR_OFFSET;
701 break;
702 case XEN_SCSI_DISK8_MAJOR:
703 case XEN_SCSI_DISK9_MAJOR:
704 case XEN_SCSI_DISK10_MAJOR:
705 case XEN_SCSI_DISK11_MAJOR:
706 case XEN_SCSI_DISK12_MAJOR:
707 case XEN_SCSI_DISK13_MAJOR:
708 case XEN_SCSI_DISK14_MAJOR:
709 case XEN_SCSI_DISK15_MAJOR:
710 *offset = (*minor / PARTS_PER_DISK) +
711 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
712 EMULATED_SD_DISK_NAME_OFFSET;
713 *minor = *minor +
714 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
715 EMULATED_SD_DISK_MINOR_OFFSET;
716 break;
717 case XENVBD_MAJOR:
718 *offset = *minor / PARTS_PER_DISK;
719 break;
720 default:
721 printk(KERN_WARNING "blkfront: your disk configuration is "
722 "incorrect, please use an xvd device instead\n");
723 return -ENODEV;
724 }
725 return 0;
726}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700727
Jan Beuliche77c78c2012-04-05 16:37:22 +0100728static char *encode_disk_name(char *ptr, unsigned int n)
729{
730 if (n >= 26)
731 ptr = encode_disk_name(ptr, n / 26 - 1);
732 *ptr = 'a' + n % 26;
733 return ptr + 1;
734}
735
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700736static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
737 struct blkfront_info *info,
738 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700739{
740 struct gendisk *gd;
741 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000742 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700743 unsigned int offset;
744 int minor;
745 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100746 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700747
748 BUG_ON(info->gd != NULL);
749 BUG_ON(info->rq != NULL);
750
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700751 if ((info->vdevice>>EXT_SHIFT) > 1) {
752 /* this is above the extended range; something is wrong */
753 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
754 return -ENODEV;
755 }
756
757 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000758 err = xen_translate_vdev(info->vdevice, &minor, &offset);
759 if (err)
760 return err;
761 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700762 } else {
763 minor = BLKIF_MINOR_EXT(info->vdevice);
764 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000765 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200766 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000767 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
768 "emulated IDE disks,\n\t choose an xvd device name"
769 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700770 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100771 if (minor >> MINORBITS) {
772 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
773 info->vdevice, minor);
774 return -ENODEV;
775 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700776
777 if ((minor % nr_parts) == 0)
778 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700779
Jan Beulich0e345822010-08-07 18:28:55 +0200780 err = xlbd_reserve_minors(minor, nr_minors);
781 if (err)
782 goto out;
783 err = -ENODEV;
784
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700785 gd = alloc_disk(nr_minors);
786 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200787 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700788
Jan Beuliche77c78c2012-04-05 16:37:22 +0100789 strcpy(gd->disk_name, DEV_NAME);
790 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
791 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
792 if (nr_minors > 1)
793 *ptr = 0;
794 else
795 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
796 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700797
798 gd->major = XENVBD_MAJOR;
799 gd->first_minor = minor;
800 gd->fops = &xlvbd_block_fops;
801 gd->private_data = info;
802 gd->driverfs_dev = &(info->xbdev->dev);
803 set_capacity(gd, capacity);
804
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200805 if (xlvbd_init_blk_queue(gd, sector_size,
806 info->max_indirect_segments ? :
807 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700808 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200809 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700810 }
811
812 info->rq = gd->queue;
813 info->gd = gd;
814
Tejun Heo4913efe2010-09-03 11:56:16 +0200815 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700816
817 if (vdisk_info & VDISK_READONLY)
818 set_disk_ro(gd, 1);
819
820 if (vdisk_info & VDISK_REMOVABLE)
821 gd->flags |= GENHD_FL_REMOVABLE;
822
823 if (vdisk_info & VDISK_CDROM)
824 gd->flags |= GENHD_FL_CD;
825
826 return 0;
827
Jan Beulich0e345822010-08-07 18:28:55 +0200828 release:
829 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700830 out:
831 return err;
832}
833
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200834static void xlvbd_release_gendisk(struct blkfront_info *info)
835{
836 unsigned int minor, nr_minors;
837 unsigned long flags;
838
839 if (info->rq == NULL)
840 return;
841
Steven Noonan34678112012-02-17 12:04:44 -0800842 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200843
844 /* No more blkif_request(). */
845 blk_stop_queue(info->rq);
846
847 /* No more gnttab callback work. */
848 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800849 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200850
851 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700852 flush_work(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200853
854 del_gendisk(info->gd);
855
856 minor = info->gd->first_minor;
857 nr_minors = info->gd->minors;
858 xlbd_release_minors(minor, nr_minors);
859
860 blk_cleanup_queue(info->rq);
861 info->rq = NULL;
862
863 put_disk(info->gd);
864 info->gd = NULL;
865}
866
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700867static void kick_pending_request_queues(struct blkfront_info *info)
868{
869 if (!RING_FULL(&info->ring)) {
870 /* Re-enable calldowns. */
871 blk_start_queue(info->rq);
872 /* Kick things off immediately. */
873 do_blkif_request(info->rq);
874 }
875}
876
877static void blkif_restart_queue(struct work_struct *work)
878{
879 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
880
Steven Noonan34678112012-02-17 12:04:44 -0800881 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700882 if (info->connected == BLKIF_STATE_CONNECTED)
883 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800884 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700885}
886
887static void blkif_free(struct blkfront_info *info, int suspend)
888{
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100889 struct grant *persistent_gnt;
890 struct grant *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200891 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200892
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700893 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800894 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700895 info->connected = suspend ?
896 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
897 /* No more blkif_request(). */
898 if (info->rq)
899 blk_stop_queue(info->rq);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200900
901 /* Remove all persistent grants */
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100902 if (!list_empty(&info->persistent_gnts)) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100903 list_for_each_entry_safe(persistent_gnt, n,
904 &info->persistent_gnts, node) {
905 list_del(&persistent_gnt->node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100906 if (persistent_gnt->gref != GRANT_INVALID_REF) {
907 gnttab_end_foreign_access(persistent_gnt->gref,
908 0, 0UL);
909 info->persistent_gnts_c--;
910 }
Roger Pau Monne07c540a2012-11-16 19:26:47 +0100911 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100912 kfree(persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200913 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200914 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100915 BUG_ON(info->persistent_gnts_c != 0);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200916
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200917 for (i = 0; i < BLK_RING_SIZE; i++) {
918 /*
919 * Clear persistent grants present in requests already
920 * on the shared ring
921 */
922 if (!info->shadow[i].request)
923 goto free_shadow;
924
925 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
926 info->shadow[i].req.u.indirect.nr_segments :
927 info->shadow[i].req.u.rw.nr_segments;
928 for (j = 0; j < segs; j++) {
929 persistent_gnt = info->shadow[i].grants_used[j];
930 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
931 __free_page(pfn_to_page(persistent_gnt->pfn));
932 kfree(persistent_gnt);
933 }
934
935 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
936 /*
937 * If this is not an indirect operation don't try to
938 * free indirect segments
939 */
940 goto free_shadow;
941
942 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
943 persistent_gnt = info->shadow[i].indirect_grants[j];
944 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
945 __free_page(pfn_to_page(persistent_gnt->pfn));
946 kfree(persistent_gnt);
947 }
948
949free_shadow:
950 kfree(info->shadow[i].grants_used);
951 info->shadow[i].grants_used = NULL;
952 kfree(info->shadow[i].indirect_grants);
953 info->shadow[i].indirect_grants = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +0200954 kfree(info->shadow[i].sg);
955 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200956 }
957
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700958 /* No more gnttab callback work. */
959 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800960 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700961
962 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700963 flush_work(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700964
965 /* Free resources associated with old device channel. */
966 if (info->ring_ref != GRANT_INVALID_REF) {
967 gnttab_end_foreign_access(info->ring_ref, 0,
968 (unsigned long)info->ring.sring);
969 info->ring_ref = GRANT_INVALID_REF;
970 info->ring.sring = NULL;
971 }
972 if (info->irq)
973 unbind_from_irqhandler(info->irq, info);
974 info->evtchn = info->irq = 0;
975
976}
977
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200978static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
979 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700980{
Roger Pau Monned62f6912012-12-07 19:00:31 +0100981 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +0200982 struct scatterlist *sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200983 char *bvec_data;
984 void *shared_data;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200985 int nseg;
986
987 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
988 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200989
990 if (bret->operation == BLKIF_OP_READ) {
991 /*
992 * Copy the data received from the backend into the bvec.
993 * Since bv_offset can be different than 0, and bv_len different
994 * than PAGE_SIZE, we have to keep track of the current offset,
995 * to be sure we are copying the data from the right shared page.
996 */
Roger Pau Monneb7649152013-05-02 10:58:50 +0200997 for_each_sg(s->sg, sg, nseg, i) {
998 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200999 shared_data = kmap_atomic(
1000 pfn_to_page(s->grants_used[i]->pfn));
Roger Pau Monneb7649152013-05-02 10:58:50 +02001001 bvec_data = kmap_atomic(sg_page(sg));
1002 memcpy(bvec_data + sg->offset,
1003 shared_data + sg->offset,
1004 sg->length);
1005 kunmap_atomic(bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001006 kunmap_atomic(shared_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001007 }
1008 }
1009 /* Add the persistent grant into the list of free grants */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001010 for (i = 0; i < nseg; i++) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001011 list_add(&s->grants_used[i]->node, &info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001012 info->persistent_gnts_c++;
1013 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001014 if (s->req.operation == BLKIF_OP_INDIRECT) {
1015 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
1016 list_add(&s->indirect_grants[i]->node, &info->persistent_gnts);
1017 info->persistent_gnts_c++;
1018 }
1019 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001020}
1021
1022static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1023{
1024 struct request *req;
1025 struct blkif_response *bret;
1026 RING_IDX i, rp;
1027 unsigned long flags;
1028 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001029 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001030
Steven Noonan34678112012-02-17 12:04:44 -08001031 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001032
1033 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -08001034 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001035 return IRQ_HANDLED;
1036 }
1037
1038 again:
1039 rp = info->ring.sring->rsp_prod;
1040 rmb(); /* Ensure we see queued responses up to 'rp'. */
1041
1042 for (i = info->ring.rsp_cons; i != rp; i++) {
1043 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001044
1045 bret = RING_GET_RESPONSE(&info->ring, i);
1046 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001047 /*
1048 * The backend has messed up and given us an id that we would
1049 * never have given to it (we stamp it up to BLK_RING_SIZE -
1050 * look in get_id_from_freelist.
1051 */
1052 if (id >= BLK_RING_SIZE) {
1053 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1054 info->gd->disk_name, op_name(bret->operation), id);
1055 /* We can't safely get the 'struct request' as
1056 * the id is busted. */
1057 continue;
1058 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001059 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001060
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001061 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001062 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001063
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001064 if (add_id_to_freelist(info, id)) {
1065 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1066 info->gd->disk_name, op_name(bret->operation), id);
1067 continue;
1068 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001069
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001070 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001071 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001072 case BLKIF_OP_DISCARD:
1073 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1074 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001075 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1076 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +08001077 error = -EOPNOTSUPP;
1078 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001079 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001080 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001081 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001082 }
1083 __blk_end_request_all(req, error);
1084 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001085 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001086 case BLKIF_OP_WRITE_BARRIER:
1087 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001088 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1089 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001090 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001091 }
1092 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001093 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001094 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1095 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001096 error = -EOPNOTSUPP;
1097 }
1098 if (unlikely(error)) {
1099 if (error == -EOPNOTSUPP)
1100 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001101 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001102 info->flush_op = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001103 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001104 }
1105 /* fall through */
1106 case BLKIF_OP_READ:
1107 case BLKIF_OP_WRITE:
1108 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1109 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1110 "request: %x\n", bret->status);
1111
Tejun Heo40cbbb72009-04-23 11:05:19 +09001112 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001113 break;
1114 default:
1115 BUG();
1116 }
1117 }
1118
1119 info->ring.rsp_cons = i;
1120
1121 if (i != info->ring.req_prod_pvt) {
1122 int more_to_do;
1123 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1124 if (more_to_do)
1125 goto again;
1126 } else
1127 info->ring.sring->rsp_event = i + 1;
1128
1129 kick_pending_request_queues(info);
1130
Steven Noonan34678112012-02-17 12:04:44 -08001131 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001132
1133 return IRQ_HANDLED;
1134}
1135
1136
1137static int setup_blkring(struct xenbus_device *dev,
1138 struct blkfront_info *info)
1139{
1140 struct blkif_sring *sring;
1141 int err;
1142
1143 info->ring_ref = GRANT_INVALID_REF;
1144
Ian Campbella144ff02008-06-17 10:47:08 +02001145 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001146 if (!sring) {
1147 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1148 return -ENOMEM;
1149 }
1150 SHARED_RING_INIT(sring);
1151 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1152
1153 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
1154 if (err < 0) {
1155 free_page((unsigned long)sring);
1156 info->ring.sring = NULL;
1157 goto fail;
1158 }
1159 info->ring_ref = err;
1160
1161 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1162 if (err)
1163 goto fail;
1164
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001165 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1166 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001167 if (err <= 0) {
1168 xenbus_dev_fatal(dev, err,
1169 "bind_evtchn_to_irqhandler failed");
1170 goto fail;
1171 }
1172 info->irq = err;
1173
1174 return 0;
1175fail:
1176 blkif_free(info, 0);
1177 return err;
1178}
1179
1180
1181/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001182static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001183 struct blkfront_info *info)
1184{
1185 const char *message = NULL;
1186 struct xenbus_transaction xbt;
1187 int err;
1188
1189 /* Create shared ring, alloc event channel. */
1190 err = setup_blkring(dev, info);
1191 if (err)
1192 goto out;
1193
1194again:
1195 err = xenbus_transaction_start(&xbt);
1196 if (err) {
1197 xenbus_dev_fatal(dev, err, "starting transaction");
1198 goto destroy_blkring;
1199 }
1200
1201 err = xenbus_printf(xbt, dev->nodename,
1202 "ring-ref", "%u", info->ring_ref);
1203 if (err) {
1204 message = "writing ring-ref";
1205 goto abort_transaction;
1206 }
1207 err = xenbus_printf(xbt, dev->nodename,
1208 "event-channel", "%u", info->evtchn);
1209 if (err) {
1210 message = "writing event-channel";
1211 goto abort_transaction;
1212 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001213 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1214 XEN_IO_PROTO_ABI_NATIVE);
1215 if (err) {
1216 message = "writing protocol";
1217 goto abort_transaction;
1218 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001219 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001220 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001221 if (err)
1222 dev_warn(&dev->dev,
1223 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001224
1225 err = xenbus_transaction_end(xbt, 0);
1226 if (err) {
1227 if (err == -EAGAIN)
1228 goto again;
1229 xenbus_dev_fatal(dev, err, "completing transaction");
1230 goto destroy_blkring;
1231 }
1232
1233 xenbus_switch_state(dev, XenbusStateInitialised);
1234
1235 return 0;
1236
1237 abort_transaction:
1238 xenbus_transaction_end(xbt, 1);
1239 if (message)
1240 xenbus_dev_fatal(dev, err, "%s", message);
1241 destroy_blkring:
1242 blkif_free(info, 0);
1243 out:
1244 return err;
1245}
1246
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001247/**
1248 * Entry point to this code when a new device is created. Allocate the basic
1249 * structures and the ring buffer for communication with the backend, and
1250 * inform the backend of the appropriate details for those. Switch to
1251 * Initialised state.
1252 */
1253static int blkfront_probe(struct xenbus_device *dev,
1254 const struct xenbus_device_id *id)
1255{
1256 int err, vdevice, i;
1257 struct blkfront_info *info;
1258
1259 /* FIXME: Use dynamic device id if this is not set. */
1260 err = xenbus_scanf(XBT_NIL, dev->nodename,
1261 "virtual-device", "%i", &vdevice);
1262 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001263 /* go looking in the extended area instead */
1264 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1265 "%i", &vdevice);
1266 if (err != 1) {
1267 xenbus_dev_fatal(dev, err, "reading virtual-device");
1268 return err;
1269 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001270 }
1271
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001272 if (xen_hvm_domain()) {
1273 char *type;
1274 int len;
1275 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +01001276 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001277 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001278
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001279 if (!VDEV_IS_EXTENDED(vdevice))
1280 major = BLKIF_MAJOR(vdevice);
1281 else
1282 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001283
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001284 if (major != XENVBD_MAJOR) {
1285 printk(KERN_INFO
1286 "%s: HVM does not support vbd %d as xen block device\n",
1287 __FUNCTION__, vdevice);
1288 return -ENODEV;
1289 }
1290 }
1291 /* do not create a PV cdrom device if we are an HVM guest */
1292 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1293 if (IS_ERR(type))
1294 return -ENODEV;
1295 if (strncmp(type, "cdrom", 5) == 0) {
1296 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001297 return -ENODEV;
1298 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001299 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001300 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001301 info = kzalloc(sizeof(*info), GFP_KERNEL);
1302 if (!info) {
1303 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1304 return -ENOMEM;
1305 }
1306
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001307 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001308 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001309 info->xbdev = dev;
1310 info->vdevice = vdevice;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001311 INIT_LIST_HEAD(&info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001312 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001313 info->connected = BLKIF_STATE_DISCONNECTED;
1314 INIT_WORK(&info->work, blkif_restart_queue);
1315
1316 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001317 info->shadow[i].req.u.rw.id = i+1;
1318 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001319
1320 /* Front end dir is a number, which is used as the id. */
1321 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001322 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001323
Ian Campbell203fd612009-12-04 15:33:54 +00001324 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001325 if (err) {
1326 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001327 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001328 return err;
1329 }
1330
1331 return 0;
1332}
1333
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001334/*
1335 * This is a clone of md_trim_bio, used to split a bio into smaller ones
1336 */
1337static void trim_bio(struct bio *bio, int offset, int size)
1338{
1339 /* 'bio' is a cloned bio which we need to trim to match
1340 * the given offset and size.
1341 * This requires adjusting bi_sector, bi_size, and bi_io_vec
1342 */
1343 int i;
1344 struct bio_vec *bvec;
1345 int sofar = 0;
1346
1347 size <<= 9;
1348 if (offset == 0 && size == bio->bi_size)
1349 return;
1350
1351 bio->bi_sector += offset;
1352 bio->bi_size = size;
1353 offset <<= 9;
1354 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1355
1356 while (bio->bi_idx < bio->bi_vcnt &&
1357 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
1358 /* remove this whole bio_vec */
1359 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
1360 bio->bi_idx++;
1361 }
1362 if (bio->bi_idx < bio->bi_vcnt) {
1363 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
1364 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
1365 }
1366 /* avoid any complications with bi_idx being non-zero*/
1367 if (bio->bi_idx) {
1368 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
1369 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
1370 bio->bi_vcnt -= bio->bi_idx;
1371 bio->bi_idx = 0;
1372 }
1373 /* Make sure vcnt and last bv are not too big */
1374 bio_for_each_segment(bvec, bio, i) {
1375 if (sofar + bvec->bv_len > size)
1376 bvec->bv_len = size - sofar;
1377 if (bvec->bv_len == 0) {
1378 bio->bi_vcnt = i;
1379 break;
1380 }
1381 sofar += bvec->bv_len;
1382 }
1383}
1384
1385static void split_bio_end(struct bio *bio, int error)
1386{
1387 struct split_bio *split_bio = bio->bi_private;
1388
1389 if (error)
1390 split_bio->err = error;
1391
1392 if (atomic_dec_and_test(&split_bio->pending)) {
1393 split_bio->bio->bi_phys_segments = 0;
1394 bio_endio(split_bio->bio, split_bio->err);
1395 kfree(split_bio);
1396 }
1397 bio_put(bio);
1398}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001399
1400static int blkif_recover(struct blkfront_info *info)
1401{
1402 int i;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001403 struct request *req, *n;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001404 struct blk_shadow *copy;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001405 int rc;
1406 struct bio *bio, *cloned_bio;
1407 struct bio_list bio_list, merge_bio;
1408 unsigned int segs, offset;
1409 int pending, size;
1410 struct split_bio *split_bio;
1411 struct list_head requests;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001412
1413 /* Stage 1: Make a safe copy of the shadow state. */
Mihnea Dobrescu-Balaur29d0b212013-03-11 13:23:36 +02001414 copy = kmemdup(info->shadow, sizeof(info->shadow),
Ian Campbella144ff02008-06-17 10:47:08 +02001415 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001416 if (!copy)
1417 return -ENOMEM;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001418
1419 /* Stage 2: Set up free list. */
1420 memset(&info->shadow, 0, sizeof(info->shadow));
1421 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001422 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001423 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001424 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001425
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001426 rc = blkfront_setup_indirect(info);
1427 if (rc) {
1428 kfree(copy);
1429 return rc;
1430 }
1431
1432 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1433 blk_queue_max_segments(info->rq, segs);
1434 bio_list_init(&bio_list);
1435 INIT_LIST_HEAD(&requests);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001436 for (i = 0; i < BLK_RING_SIZE; i++) {
1437 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001438 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001439 continue;
1440
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001441 /*
1442 * Get the bios in the request so we can re-queue them.
1443 */
1444 if (copy[i].request->cmd_flags &
1445 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1446 /*
1447 * Flush operations don't contain bios, so
1448 * we need to requeue the whole request
1449 */
1450 list_add(&copy[i].request->queuelist, &requests);
1451 continue;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001452 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001453 merge_bio.head = copy[i].request->bio;
1454 merge_bio.tail = copy[i].request->biotail;
1455 bio_list_merge(&bio_list, &merge_bio);
1456 copy[i].request->bio = NULL;
1457 blk_put_request(copy[i].request);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001458 }
1459
1460 kfree(copy);
1461
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001462 /*
1463 * Empty the queue, this is important because we might have
1464 * requests in the queue with more segments than what we
1465 * can handle now.
1466 */
1467 spin_lock_irq(&info->io_lock);
1468 while ((req = blk_fetch_request(info->rq)) != NULL) {
1469 if (req->cmd_flags &
1470 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1471 list_add(&req->queuelist, &requests);
1472 continue;
1473 }
1474 merge_bio.head = req->bio;
1475 merge_bio.tail = req->biotail;
1476 bio_list_merge(&bio_list, &merge_bio);
1477 req->bio = NULL;
1478 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1479 pr_alert("diskcache flush request found!\n");
1480 __blk_put_request(info->rq, req);
1481 }
1482 spin_unlock_irq(&info->io_lock);
1483
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001484 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1485
Steven Noonan34678112012-02-17 12:04:44 -08001486 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001487
1488 /* Now safe for us to use the shared ring */
1489 info->connected = BLKIF_STATE_CONNECTED;
1490
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001491 /* Kick any other new requests queued since we resumed */
1492 kick_pending_request_queues(info);
1493
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001494 list_for_each_entry_safe(req, n, &requests, queuelist) {
1495 /* Requeue pending requests (flush or discard) */
1496 list_del_init(&req->queuelist);
1497 BUG_ON(req->nr_phys_segments > segs);
1498 blk_requeue_request(info->rq, req);
1499 }
Steven Noonan34678112012-02-17 12:04:44 -08001500 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001501
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001502 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1503 /* Traverse the list of pending bios and re-queue them */
1504 if (bio_segments(bio) > segs) {
1505 /*
1506 * This bio has more segments than what we can
1507 * handle, we have to split it.
1508 */
1509 pending = (bio_segments(bio) + segs - 1) / segs;
1510 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1511 BUG_ON(split_bio == NULL);
1512 atomic_set(&split_bio->pending, pending);
1513 split_bio->bio = bio;
1514 for (i = 0; i < pending; i++) {
1515 offset = (i * segs * PAGE_SIZE) >> 9;
1516 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
1517 (unsigned int)(bio->bi_size >> 9) - offset);
1518 cloned_bio = bio_clone(bio, GFP_NOIO);
1519 BUG_ON(cloned_bio == NULL);
1520 trim_bio(cloned_bio, offset, size);
1521 cloned_bio->bi_private = split_bio;
1522 cloned_bio->bi_end_io = split_bio_end;
1523 submit_bio(cloned_bio->bi_rw, cloned_bio);
1524 }
1525 /*
1526 * Now we have to wait for all those smaller bios to
1527 * end, so we can also end the "parent" bio.
1528 */
1529 continue;
1530 }
1531 /* We don't need to split this bio */
1532 submit_bio(bio->bi_rw, bio);
1533 }
1534
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001535 return 0;
1536}
1537
1538/**
1539 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1540 * driver restart. We tear down our blkif structure and recreate it, but
1541 * leave the device-layer structures intact so that this is transparent to the
1542 * rest of the kernel.
1543 */
1544static int blkfront_resume(struct xenbus_device *dev)
1545{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001546 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001547 int err;
1548
1549 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1550
1551 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1552
Ian Campbell203fd612009-12-04 15:33:54 +00001553 err = talk_to_blkback(dev, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001554
1555 /*
1556 * We have to wait for the backend to switch to
1557 * connected state, since we want to read which
1558 * features it supports.
1559 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001560
1561 return err;
1562}
1563
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001564static void
1565blkfront_closing(struct blkfront_info *info)
1566{
1567 struct xenbus_device *xbdev = info->xbdev;
1568 struct block_device *bdev = NULL;
1569
1570 mutex_lock(&info->mutex);
1571
1572 if (xbdev->state == XenbusStateClosing) {
1573 mutex_unlock(&info->mutex);
1574 return;
1575 }
1576
1577 if (info->gd)
1578 bdev = bdget_disk(info->gd, 0);
1579
1580 mutex_unlock(&info->mutex);
1581
1582 if (!bdev) {
1583 xenbus_frontend_closed(xbdev);
1584 return;
1585 }
1586
1587 mutex_lock(&bdev->bd_mutex);
1588
Daniel Stodden7b32d102010-04-30 22:01:23 +00001589 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001590 xenbus_dev_error(xbdev, -EBUSY,
1591 "Device in use; refusing to close");
1592 xenbus_switch_state(xbdev, XenbusStateClosing);
1593 } else {
1594 xlvbd_release_gendisk(info);
1595 xenbus_frontend_closed(xbdev);
1596 }
1597
1598 mutex_unlock(&bdev->bd_mutex);
1599 bdput(bdev);
1600}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001601
Li Dongyanged30bf32011-09-01 18:39:09 +08001602static void blkfront_setup_discard(struct blkfront_info *info)
1603{
1604 int err;
1605 char *type;
1606 unsigned int discard_granularity;
1607 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001608 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001609
1610 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1611 if (IS_ERR(type))
1612 return;
1613
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001614 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001615 if (strncmp(type, "phy", 3) == 0) {
1616 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1617 "discard-granularity", "%u", &discard_granularity,
1618 "discard-alignment", "%u", &discard_alignment,
1619 NULL);
1620 if (!err) {
1621 info->feature_discard = 1;
1622 info->discard_granularity = discard_granularity;
1623 info->discard_alignment = discard_alignment;
1624 }
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001625 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1626 "discard-secure", "%d", &discard_secure,
1627 NULL);
1628 if (!err)
1629 info->feature_secdiscard = discard_secure;
1630
Li Dongyanged30bf32011-09-01 18:39:09 +08001631 } else if (strncmp(type, "file", 4) == 0)
1632 info->feature_discard = 1;
1633
1634 kfree(type);
1635}
1636
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001637static int blkfront_setup_indirect(struct blkfront_info *info)
1638{
1639 unsigned int indirect_segments, segs;
1640 int err, i;
1641
1642 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1643 "feature-max-indirect-segments", "%u", &indirect_segments,
1644 NULL);
1645 if (err) {
1646 info->max_indirect_segments = 0;
1647 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1648 } else {
1649 info->max_indirect_segments = min(indirect_segments,
1650 xen_blkif_max_segments);
1651 segs = info->max_indirect_segments;
1652 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001653
1654 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
1655 if (err)
1656 goto out_of_memory;
1657
1658 for (i = 0; i < BLK_RING_SIZE; i++) {
1659 info->shadow[i].grants_used = kzalloc(
1660 sizeof(info->shadow[i].grants_used[0]) * segs,
1661 GFP_NOIO);
Roger Pau Monneb7649152013-05-02 10:58:50 +02001662 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001663 if (info->max_indirect_segments)
1664 info->shadow[i].indirect_grants = kzalloc(
1665 sizeof(info->shadow[i].indirect_grants[0]) *
1666 INDIRECT_GREFS(segs),
1667 GFP_NOIO);
1668 if ((info->shadow[i].grants_used == NULL) ||
Roger Pau Monneb7649152013-05-02 10:58:50 +02001669 (info->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001670 (info->max_indirect_segments &&
1671 (info->shadow[i].indirect_grants == NULL)))
1672 goto out_of_memory;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001673 sg_init_table(info->shadow[i].sg, segs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001674 }
1675
1676
1677 return 0;
1678
1679out_of_memory:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001680 for (i = 0; i < BLK_RING_SIZE; i++) {
1681 kfree(info->shadow[i].grants_used);
1682 info->shadow[i].grants_used = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001683 kfree(info->shadow[i].sg);
1684 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001685 kfree(info->shadow[i].indirect_grants);
1686 info->shadow[i].indirect_grants = NULL;
1687 }
1688 return -ENOMEM;
1689}
1690
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001691/*
1692 * Invoked when the backend is finally 'ready' (and has told produced
1693 * the details about the physical device - #sectors, size, etc).
1694 */
1695static void blkfront_connect(struct blkfront_info *info)
1696{
1697 unsigned long long sectors;
1698 unsigned long sector_size;
1699 unsigned int binfo;
1700 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001701 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001702
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001703 switch (info->connected) {
1704 case BLKIF_STATE_CONNECTED:
1705 /*
1706 * Potentially, the back-end may be signalling
1707 * a capacity change; update the capacity.
1708 */
1709 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1710 "sectors", "%Lu", &sectors);
1711 if (XENBUS_EXIST_ERR(err))
1712 return;
1713 printk(KERN_INFO "Setting capacity to %Lu\n",
1714 sectors);
1715 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001716 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001717
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001718 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001719 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001720 /*
1721 * If we are recovering from suspension, we need to wait
1722 * for the backend to announce it's features before
1723 * reconnecting, at least we need to know if the backend
1724 * supports indirect descriptors, and how many.
1725 */
1726 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001727 return;
1728
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001729 default:
1730 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001731 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001732
1733 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1734 __func__, info->xbdev->otherend);
1735
1736 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1737 "sectors", "%llu", &sectors,
1738 "info", "%u", &binfo,
1739 "sector-size", "%lu", &sector_size,
1740 NULL);
1741 if (err) {
1742 xenbus_dev_fatal(info->xbdev, err,
1743 "reading backend fields at %s",
1744 info->xbdev->otherend);
1745 return;
1746 }
1747
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001748 info->feature_flush = 0;
1749 info->flush_op = 0;
1750
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001751 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001752 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001753 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001754
1755 /*
1756 * If there's no "feature-barrier" defined, then it means
1757 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001758 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001759 *
Tejun Heo6958f142010-09-03 11:56:16 +02001760 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001761 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001762 if (!err && barrier) {
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001763 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001764 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1765 }
1766 /*
1767 * And if there is "feature-flush-cache" use that above
1768 * barriers.
1769 */
1770 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1771 "feature-flush-cache", "%d", &flush,
1772 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001773
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001774 if (!err && flush) {
1775 info->feature_flush = REQ_FLUSH;
1776 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1777 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001778
1779 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1780 "feature-discard", "%d", &discard,
1781 NULL);
1782
1783 if (!err && discard)
1784 blkfront_setup_discard(info);
1785
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001786 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1787 "feature-persistent", "%u", &persistent,
1788 NULL);
1789 if (err)
1790 info->feature_persistent = 0;
1791 else
1792 info->feature_persistent = persistent;
1793
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001794 err = blkfront_setup_indirect(info);
1795 if (err) {
1796 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1797 info->xbdev->otherend);
1798 return;
1799 }
1800
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001801 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001802 if (err) {
1803 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1804 info->xbdev->otherend);
1805 return;
1806 }
1807
1808 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1809
1810 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001811 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001812 info->connected = BLKIF_STATE_CONNECTED;
1813 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001814 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001815
1816 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001817
1818 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001819}
1820
1821/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001822 * Callback received when the backend's state changes.
1823 */
Ian Campbell203fd612009-12-04 15:33:54 +00001824static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001825 enum xenbus_state backend_state)
1826{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001827 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001828
Ian Campbell203fd612009-12-04 15:33:54 +00001829 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001830
1831 switch (backend_state) {
1832 case XenbusStateInitialising:
1833 case XenbusStateInitWait:
1834 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001835 case XenbusStateReconfiguring:
1836 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001837 case XenbusStateUnknown:
1838 case XenbusStateClosed:
1839 break;
1840
1841 case XenbusStateConnected:
1842 blkfront_connect(info);
1843 break;
1844
1845 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001846 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001847 break;
1848 }
1849}
1850
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001851static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001852{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001853 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1854 struct block_device *bdev = NULL;
1855 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001856
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001857 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001858
1859 blkif_free(info, 0);
1860
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001861 mutex_lock(&info->mutex);
1862
1863 disk = info->gd;
1864 if (disk)
1865 bdev = bdget_disk(disk, 0);
1866
1867 info->xbdev = NULL;
1868 mutex_unlock(&info->mutex);
1869
1870 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001871 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001872 return 0;
1873 }
1874
1875 /*
1876 * The xbdev was removed before we reached the Closed
1877 * state. See if it's safe to remove the disk. If the bdev
1878 * isn't closed yet, we let release take care of it.
1879 */
1880
1881 mutex_lock(&bdev->bd_mutex);
1882 info = disk->private_data;
1883
Daniel Stoddend54142c2010-08-07 18:51:21 +02001884 dev_warn(disk_to_dev(disk),
1885 "%s was hot-unplugged, %d stale handles\n",
1886 xbdev->nodename, bdev->bd_openers);
1887
Daniel Stodden7b32d102010-04-30 22:01:23 +00001888 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001889 xlvbd_release_gendisk(info);
1890 disk->private_data = NULL;
1891 kfree(info);
1892 }
1893
1894 mutex_unlock(&bdev->bd_mutex);
1895 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001896
1897 return 0;
1898}
1899
Christian Limpach1d78d702008-04-02 10:54:04 -07001900static int blkfront_is_ready(struct xenbus_device *dev)
1901{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001902 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001903
Jan Beulich5d7ed202010-08-07 18:31:12 +02001904 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001905}
1906
Al Viroa63c8482008-03-02 10:23:47 -05001907static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001908{
Daniel Stodden13961742010-08-07 18:36:53 +02001909 struct gendisk *disk = bdev->bd_disk;
1910 struct blkfront_info *info;
1911 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001912
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001913 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001914
Daniel Stodden13961742010-08-07 18:36:53 +02001915 info = disk->private_data;
1916 if (!info) {
1917 /* xbdev gone */
1918 err = -ERESTARTSYS;
1919 goto out;
1920 }
1921
1922 mutex_lock(&info->mutex);
1923
1924 if (!info->gd)
1925 /* xbdev is closed */
1926 err = -ERESTARTSYS;
1927
1928 mutex_unlock(&info->mutex);
1929
Daniel Stodden13961742010-08-07 18:36:53 +02001930out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001931 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001932 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001933}
1934
Al Viroa63c8482008-03-02 10:23:47 -05001935static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001936{
Al Viroa63c8482008-03-02 10:23:47 -05001937 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001938 struct block_device *bdev;
1939 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001940
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001941 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001942
1943 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001944
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001945 if (bdev->bd_openers)
1946 goto out;
1947
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001948 /*
1949 * Check if we have been instructed to close. We will have
1950 * deferred this request, because the bdev was still open.
1951 */
1952
1953 mutex_lock(&info->mutex);
1954 xbdev = info->xbdev;
1955
1956 if (xbdev && xbdev->state == XenbusStateClosing) {
1957 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001958 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001959 xlvbd_release_gendisk(info);
1960 xenbus_frontend_closed(info->xbdev);
1961 }
1962
1963 mutex_unlock(&info->mutex);
1964
1965 if (!xbdev) {
1966 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001967 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001968 xlvbd_release_gendisk(info);
1969 disk->private_data = NULL;
1970 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001971 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001972
Jens Axboea4cc14e2010-08-08 21:50:05 -04001973out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01001974 bdput(bdev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001975 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001976 return 0;
1977}
1978
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001979static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001980{
1981 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001982 .open = blkif_open,
1983 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001984 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001985 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001986};
1987
1988
Márton Némethec9c42e2010-01-10 13:39:52 +01001989static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001990 { "vbd" },
1991 { "" }
1992};
1993
Jan Beulich73db1442011-12-22 09:08:13 +00001994static DEFINE_XENBUS_DRIVER(blkfront, ,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001995 .probe = blkfront_probe,
1996 .remove = blkfront_remove,
1997 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001998 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001999 .is_ready = blkfront_is_ready,
Jan Beulich73db1442011-12-22 09:08:13 +00002000);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002001
2002static int __init xlblk_init(void)
2003{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002004 int ret;
2005
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002006 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002007 return -ENODEV;
2008
Igor Mammedove95ae5a2012-03-27 19:31:08 +02002009 if (xen_hvm_domain() && !xen_platform_pci_unplug)
Igor Mammedovb9136d22012-03-21 15:08:38 +01002010 return -ENODEV;
2011
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002012 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2013 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2014 XENVBD_MAJOR, DEV_NAME);
2015 return -ENODEV;
2016 }
2017
Jan Beulich73db1442011-12-22 09:08:13 +00002018 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002019 if (ret) {
2020 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2021 return ret;
2022 }
2023
2024 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002025}
2026module_init(xlblk_init);
2027
2028
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002029static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002030{
Jan Beulich86050672012-04-05 16:04:52 +01002031 xenbus_unregister_driver(&blkfront_driver);
2032 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2033 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002034}
2035module_exit(xlblk_exit);
2036
2037MODULE_DESCRIPTION("Xen virtual block device frontend");
2038MODULE_LICENSE("GPL");
2039MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002040MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f092008-04-02 10:54:06 -07002041MODULE_ALIAS("xenblk");