blob: 82d63d5b1750d1ac8c54f7c366edceae36367a9a [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;
79};
80
81struct split_bio {
82 struct bio *bio;
83 atomic_t pending;
84 int err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085};
86
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020087static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070088static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070089
Roger Pau Monne402b27f2013-04-18 16:06:54 +020090/*
91 * Maximum number of segments in indirect requests, the actual value used by
92 * the frontend driver is the minimum of this value and the value provided
93 * by the backend driver.
94 */
95
96static unsigned int xen_blkif_max_segments = 32;
97
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080098#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070099
100/*
101 * We have one of these per vbd, whether ide, scsi or 'other'. They
102 * hang in private_data off the gendisk structure. We may end up
103 * putting all kinds of interesting stuff here :-)
104 */
105struct blkfront_info
106{
Steven Noonan34678112012-02-17 12:04:44 -0800107 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000108 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700109 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700110 struct gendisk *gd;
111 int vdevice;
112 blkif_vdev_t handle;
113 enum blkif_state connected;
114 int ring_ref;
115 struct blkif_front_ring ring;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200116 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700117 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);
441 nseg = blk_rq_map_sg(req->q, req, info->sg);
442 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 }
472 for_each_sg(info->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 kfree(info->sg);
918 info->sg = NULL;
919 for (i = 0; i < BLK_RING_SIZE; i++) {
920 /*
921 * Clear persistent grants present in requests already
922 * on the shared ring
923 */
924 if (!info->shadow[i].request)
925 goto free_shadow;
926
927 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
928 info->shadow[i].req.u.indirect.nr_segments :
929 info->shadow[i].req.u.rw.nr_segments;
930 for (j = 0; j < segs; j++) {
931 persistent_gnt = info->shadow[i].grants_used[j];
932 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
933 __free_page(pfn_to_page(persistent_gnt->pfn));
934 kfree(persistent_gnt);
935 }
936
937 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
938 /*
939 * If this is not an indirect operation don't try to
940 * free indirect segments
941 */
942 goto free_shadow;
943
944 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
945 persistent_gnt = info->shadow[i].indirect_grants[j];
946 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
947 __free_page(pfn_to_page(persistent_gnt->pfn));
948 kfree(persistent_gnt);
949 }
950
951free_shadow:
952 kfree(info->shadow[i].grants_used);
953 info->shadow[i].grants_used = NULL;
954 kfree(info->shadow[i].indirect_grants);
955 info->shadow[i].indirect_grants = NULL;
956 }
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 Monne0a8704a2012-10-24 18:58:45 +0200982 struct bio_vec *bvec;
983 struct req_iterator iter;
984 unsigned long flags;
985 char *bvec_data;
986 void *shared_data;
987 unsigned int offset = 0;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200988 int nseg;
989
990 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
991 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200992
993 if (bret->operation == BLKIF_OP_READ) {
994 /*
995 * Copy the data received from the backend into the bvec.
996 * Since bv_offset can be different than 0, and bv_len different
997 * than PAGE_SIZE, we have to keep track of the current offset,
998 * to be sure we are copying the data from the right shared page.
999 */
1000 rq_for_each_segment(bvec, s->request, iter) {
1001 BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
Roger Pau Monned62f6912012-12-07 19:00:31 +01001002 if (bvec->bv_offset < offset)
1003 i++;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001004 BUG_ON(i >= nseg);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001005 shared_data = kmap_atomic(
1006 pfn_to_page(s->grants_used[i]->pfn));
1007 bvec_data = bvec_kmap_irq(bvec, &flags);
1008 memcpy(bvec_data, shared_data + bvec->bv_offset,
1009 bvec->bv_len);
1010 bvec_kunmap_irq(bvec_data, &flags);
1011 kunmap_atomic(shared_data);
Roger Pau Monned62f6912012-12-07 19:00:31 +01001012 offset = bvec->bv_offset + bvec->bv_len;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001013 }
1014 }
1015 /* Add the persistent grant into the list of free grants */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001016 for (i = 0; i < nseg; i++) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001017 list_add(&s->grants_used[i]->node, &info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001018 info->persistent_gnts_c++;
1019 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001020 if (s->req.operation == BLKIF_OP_INDIRECT) {
1021 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
1022 list_add(&s->indirect_grants[i]->node, &info->persistent_gnts);
1023 info->persistent_gnts_c++;
1024 }
1025 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001026}
1027
1028static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1029{
1030 struct request *req;
1031 struct blkif_response *bret;
1032 RING_IDX i, rp;
1033 unsigned long flags;
1034 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001035 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001036
Steven Noonan34678112012-02-17 12:04:44 -08001037 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001038
1039 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -08001040 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001041 return IRQ_HANDLED;
1042 }
1043
1044 again:
1045 rp = info->ring.sring->rsp_prod;
1046 rmb(); /* Ensure we see queued responses up to 'rp'. */
1047
1048 for (i = info->ring.rsp_cons; i != rp; i++) {
1049 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001050
1051 bret = RING_GET_RESPONSE(&info->ring, i);
1052 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001053 /*
1054 * The backend has messed up and given us an id that we would
1055 * never have given to it (we stamp it up to BLK_RING_SIZE -
1056 * look in get_id_from_freelist.
1057 */
1058 if (id >= BLK_RING_SIZE) {
1059 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1060 info->gd->disk_name, op_name(bret->operation), id);
1061 /* We can't safely get the 'struct request' as
1062 * the id is busted. */
1063 continue;
1064 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001065 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001066
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001067 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001068 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001069
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001070 if (add_id_to_freelist(info, id)) {
1071 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1072 info->gd->disk_name, op_name(bret->operation), id);
1073 continue;
1074 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001075
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001076 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001077 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001078 case BLKIF_OP_DISCARD:
1079 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1080 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001081 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1082 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +08001083 error = -EOPNOTSUPP;
1084 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001085 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001086 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001087 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001088 }
1089 __blk_end_request_all(req, error);
1090 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001091 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001092 case BLKIF_OP_WRITE_BARRIER:
1093 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001094 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1095 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001096 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001097 }
1098 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001099 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001100 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1101 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001102 error = -EOPNOTSUPP;
1103 }
1104 if (unlikely(error)) {
1105 if (error == -EOPNOTSUPP)
1106 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001107 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001108 info->flush_op = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001109 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001110 }
1111 /* fall through */
1112 case BLKIF_OP_READ:
1113 case BLKIF_OP_WRITE:
1114 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1115 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1116 "request: %x\n", bret->status);
1117
Tejun Heo40cbbb72009-04-23 11:05:19 +09001118 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001119 break;
1120 default:
1121 BUG();
1122 }
1123 }
1124
1125 info->ring.rsp_cons = i;
1126
1127 if (i != info->ring.req_prod_pvt) {
1128 int more_to_do;
1129 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1130 if (more_to_do)
1131 goto again;
1132 } else
1133 info->ring.sring->rsp_event = i + 1;
1134
1135 kick_pending_request_queues(info);
1136
Steven Noonan34678112012-02-17 12:04:44 -08001137 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001138
1139 return IRQ_HANDLED;
1140}
1141
1142
1143static int setup_blkring(struct xenbus_device *dev,
1144 struct blkfront_info *info)
1145{
1146 struct blkif_sring *sring;
1147 int err;
1148
1149 info->ring_ref = GRANT_INVALID_REF;
1150
Ian Campbella144ff02008-06-17 10:47:08 +02001151 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001152 if (!sring) {
1153 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1154 return -ENOMEM;
1155 }
1156 SHARED_RING_INIT(sring);
1157 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1158
1159 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
1160 if (err < 0) {
1161 free_page((unsigned long)sring);
1162 info->ring.sring = NULL;
1163 goto fail;
1164 }
1165 info->ring_ref = err;
1166
1167 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1168 if (err)
1169 goto fail;
1170
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001171 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1172 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001173 if (err <= 0) {
1174 xenbus_dev_fatal(dev, err,
1175 "bind_evtchn_to_irqhandler failed");
1176 goto fail;
1177 }
1178 info->irq = err;
1179
1180 return 0;
1181fail:
1182 blkif_free(info, 0);
1183 return err;
1184}
1185
1186
1187/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001188static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001189 struct blkfront_info *info)
1190{
1191 const char *message = NULL;
1192 struct xenbus_transaction xbt;
1193 int err;
1194
1195 /* Create shared ring, alloc event channel. */
1196 err = setup_blkring(dev, info);
1197 if (err)
1198 goto out;
1199
1200again:
1201 err = xenbus_transaction_start(&xbt);
1202 if (err) {
1203 xenbus_dev_fatal(dev, err, "starting transaction");
1204 goto destroy_blkring;
1205 }
1206
1207 err = xenbus_printf(xbt, dev->nodename,
1208 "ring-ref", "%u", info->ring_ref);
1209 if (err) {
1210 message = "writing ring-ref";
1211 goto abort_transaction;
1212 }
1213 err = xenbus_printf(xbt, dev->nodename,
1214 "event-channel", "%u", info->evtchn);
1215 if (err) {
1216 message = "writing event-channel";
1217 goto abort_transaction;
1218 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001219 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1220 XEN_IO_PROTO_ABI_NATIVE);
1221 if (err) {
1222 message = "writing protocol";
1223 goto abort_transaction;
1224 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001225 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001226 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001227 if (err)
1228 dev_warn(&dev->dev,
1229 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001230
1231 err = xenbus_transaction_end(xbt, 0);
1232 if (err) {
1233 if (err == -EAGAIN)
1234 goto again;
1235 xenbus_dev_fatal(dev, err, "completing transaction");
1236 goto destroy_blkring;
1237 }
1238
1239 xenbus_switch_state(dev, XenbusStateInitialised);
1240
1241 return 0;
1242
1243 abort_transaction:
1244 xenbus_transaction_end(xbt, 1);
1245 if (message)
1246 xenbus_dev_fatal(dev, err, "%s", message);
1247 destroy_blkring:
1248 blkif_free(info, 0);
1249 out:
1250 return err;
1251}
1252
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001253/**
1254 * Entry point to this code when a new device is created. Allocate the basic
1255 * structures and the ring buffer for communication with the backend, and
1256 * inform the backend of the appropriate details for those. Switch to
1257 * Initialised state.
1258 */
1259static int blkfront_probe(struct xenbus_device *dev,
1260 const struct xenbus_device_id *id)
1261{
1262 int err, vdevice, i;
1263 struct blkfront_info *info;
1264
1265 /* FIXME: Use dynamic device id if this is not set. */
1266 err = xenbus_scanf(XBT_NIL, dev->nodename,
1267 "virtual-device", "%i", &vdevice);
1268 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001269 /* go looking in the extended area instead */
1270 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1271 "%i", &vdevice);
1272 if (err != 1) {
1273 xenbus_dev_fatal(dev, err, "reading virtual-device");
1274 return err;
1275 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001276 }
1277
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001278 if (xen_hvm_domain()) {
1279 char *type;
1280 int len;
1281 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +01001282 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001283 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001284
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001285 if (!VDEV_IS_EXTENDED(vdevice))
1286 major = BLKIF_MAJOR(vdevice);
1287 else
1288 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001289
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001290 if (major != XENVBD_MAJOR) {
1291 printk(KERN_INFO
1292 "%s: HVM does not support vbd %d as xen block device\n",
1293 __FUNCTION__, vdevice);
1294 return -ENODEV;
1295 }
1296 }
1297 /* do not create a PV cdrom device if we are an HVM guest */
1298 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1299 if (IS_ERR(type))
1300 return -ENODEV;
1301 if (strncmp(type, "cdrom", 5) == 0) {
1302 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001303 return -ENODEV;
1304 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001305 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001306 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001307 info = kzalloc(sizeof(*info), GFP_KERNEL);
1308 if (!info) {
1309 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1310 return -ENOMEM;
1311 }
1312
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001313 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001314 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001315 info->xbdev = dev;
1316 info->vdevice = vdevice;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001317 INIT_LIST_HEAD(&info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001318 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001319 info->connected = BLKIF_STATE_DISCONNECTED;
1320 INIT_WORK(&info->work, blkif_restart_queue);
1321
1322 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001323 info->shadow[i].req.u.rw.id = i+1;
1324 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001325
1326 /* Front end dir is a number, which is used as the id. */
1327 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001328 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001329
Ian Campbell203fd612009-12-04 15:33:54 +00001330 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001331 if (err) {
1332 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001333 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001334 return err;
1335 }
1336
1337 return 0;
1338}
1339
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001340/*
1341 * This is a clone of md_trim_bio, used to split a bio into smaller ones
1342 */
1343static void trim_bio(struct bio *bio, int offset, int size)
1344{
1345 /* 'bio' is a cloned bio which we need to trim to match
1346 * the given offset and size.
1347 * This requires adjusting bi_sector, bi_size, and bi_io_vec
1348 */
1349 int i;
1350 struct bio_vec *bvec;
1351 int sofar = 0;
1352
1353 size <<= 9;
1354 if (offset == 0 && size == bio->bi_size)
1355 return;
1356
1357 bio->bi_sector += offset;
1358 bio->bi_size = size;
1359 offset <<= 9;
1360 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1361
1362 while (bio->bi_idx < bio->bi_vcnt &&
1363 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
1364 /* remove this whole bio_vec */
1365 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
1366 bio->bi_idx++;
1367 }
1368 if (bio->bi_idx < bio->bi_vcnt) {
1369 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
1370 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
1371 }
1372 /* avoid any complications with bi_idx being non-zero*/
1373 if (bio->bi_idx) {
1374 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
1375 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
1376 bio->bi_vcnt -= bio->bi_idx;
1377 bio->bi_idx = 0;
1378 }
1379 /* Make sure vcnt and last bv are not too big */
1380 bio_for_each_segment(bvec, bio, i) {
1381 if (sofar + bvec->bv_len > size)
1382 bvec->bv_len = size - sofar;
1383 if (bvec->bv_len == 0) {
1384 bio->bi_vcnt = i;
1385 break;
1386 }
1387 sofar += bvec->bv_len;
1388 }
1389}
1390
1391static void split_bio_end(struct bio *bio, int error)
1392{
1393 struct split_bio *split_bio = bio->bi_private;
1394
1395 if (error)
1396 split_bio->err = error;
1397
1398 if (atomic_dec_and_test(&split_bio->pending)) {
1399 split_bio->bio->bi_phys_segments = 0;
1400 bio_endio(split_bio->bio, split_bio->err);
1401 kfree(split_bio);
1402 }
1403 bio_put(bio);
1404}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001405
1406static int blkif_recover(struct blkfront_info *info)
1407{
1408 int i;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001409 struct request *req, *n;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001410 struct blk_shadow *copy;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001411 int rc;
1412 struct bio *bio, *cloned_bio;
1413 struct bio_list bio_list, merge_bio;
1414 unsigned int segs, offset;
1415 int pending, size;
1416 struct split_bio *split_bio;
1417 struct list_head requests;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001418
1419 /* Stage 1: Make a safe copy of the shadow state. */
Mihnea Dobrescu-Balaur29d0b212013-03-11 13:23:36 +02001420 copy = kmemdup(info->shadow, sizeof(info->shadow),
Ian Campbella144ff02008-06-17 10:47:08 +02001421 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001422 if (!copy)
1423 return -ENOMEM;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001424
1425 /* Stage 2: Set up free list. */
1426 memset(&info->shadow, 0, sizeof(info->shadow));
1427 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001428 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001429 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001430 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001431
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001432 rc = blkfront_setup_indirect(info);
1433 if (rc) {
1434 kfree(copy);
1435 return rc;
1436 }
1437
1438 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1439 blk_queue_max_segments(info->rq, segs);
1440 bio_list_init(&bio_list);
1441 INIT_LIST_HEAD(&requests);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001442 for (i = 0; i < BLK_RING_SIZE; i++) {
1443 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001444 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001445 continue;
1446
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001447 /*
1448 * Get the bios in the request so we can re-queue them.
1449 */
1450 if (copy[i].request->cmd_flags &
1451 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1452 /*
1453 * Flush operations don't contain bios, so
1454 * we need to requeue the whole request
1455 */
1456 list_add(&copy[i].request->queuelist, &requests);
1457 continue;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001458 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001459 merge_bio.head = copy[i].request->bio;
1460 merge_bio.tail = copy[i].request->biotail;
1461 bio_list_merge(&bio_list, &merge_bio);
1462 copy[i].request->bio = NULL;
1463 blk_put_request(copy[i].request);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001464 }
1465
1466 kfree(copy);
1467
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001468 /*
1469 * Empty the queue, this is important because we might have
1470 * requests in the queue with more segments than what we
1471 * can handle now.
1472 */
1473 spin_lock_irq(&info->io_lock);
1474 while ((req = blk_fetch_request(info->rq)) != NULL) {
1475 if (req->cmd_flags &
1476 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1477 list_add(&req->queuelist, &requests);
1478 continue;
1479 }
1480 merge_bio.head = req->bio;
1481 merge_bio.tail = req->biotail;
1482 bio_list_merge(&bio_list, &merge_bio);
1483 req->bio = NULL;
1484 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1485 pr_alert("diskcache flush request found!\n");
1486 __blk_put_request(info->rq, req);
1487 }
1488 spin_unlock_irq(&info->io_lock);
1489
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001490 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1491
Steven Noonan34678112012-02-17 12:04:44 -08001492 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001493
1494 /* Now safe for us to use the shared ring */
1495 info->connected = BLKIF_STATE_CONNECTED;
1496
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001497 /* Kick any other new requests queued since we resumed */
1498 kick_pending_request_queues(info);
1499
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001500 list_for_each_entry_safe(req, n, &requests, queuelist) {
1501 /* Requeue pending requests (flush or discard) */
1502 list_del_init(&req->queuelist);
1503 BUG_ON(req->nr_phys_segments > segs);
1504 blk_requeue_request(info->rq, req);
1505 }
Steven Noonan34678112012-02-17 12:04:44 -08001506 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001507
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001508 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1509 /* Traverse the list of pending bios and re-queue them */
1510 if (bio_segments(bio) > segs) {
1511 /*
1512 * This bio has more segments than what we can
1513 * handle, we have to split it.
1514 */
1515 pending = (bio_segments(bio) + segs - 1) / segs;
1516 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1517 BUG_ON(split_bio == NULL);
1518 atomic_set(&split_bio->pending, pending);
1519 split_bio->bio = bio;
1520 for (i = 0; i < pending; i++) {
1521 offset = (i * segs * PAGE_SIZE) >> 9;
1522 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
1523 (unsigned int)(bio->bi_size >> 9) - offset);
1524 cloned_bio = bio_clone(bio, GFP_NOIO);
1525 BUG_ON(cloned_bio == NULL);
1526 trim_bio(cloned_bio, offset, size);
1527 cloned_bio->bi_private = split_bio;
1528 cloned_bio->bi_end_io = split_bio_end;
1529 submit_bio(cloned_bio->bi_rw, cloned_bio);
1530 }
1531 /*
1532 * Now we have to wait for all those smaller bios to
1533 * end, so we can also end the "parent" bio.
1534 */
1535 continue;
1536 }
1537 /* We don't need to split this bio */
1538 submit_bio(bio->bi_rw, bio);
1539 }
1540
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001541 return 0;
1542}
1543
1544/**
1545 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1546 * driver restart. We tear down our blkif structure and recreate it, but
1547 * leave the device-layer structures intact so that this is transparent to the
1548 * rest of the kernel.
1549 */
1550static int blkfront_resume(struct xenbus_device *dev)
1551{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001552 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001553 int err;
1554
1555 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1556
1557 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1558
Ian Campbell203fd612009-12-04 15:33:54 +00001559 err = talk_to_blkback(dev, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001560
1561 /*
1562 * We have to wait for the backend to switch to
1563 * connected state, since we want to read which
1564 * features it supports.
1565 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001566
1567 return err;
1568}
1569
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001570static void
1571blkfront_closing(struct blkfront_info *info)
1572{
1573 struct xenbus_device *xbdev = info->xbdev;
1574 struct block_device *bdev = NULL;
1575
1576 mutex_lock(&info->mutex);
1577
1578 if (xbdev->state == XenbusStateClosing) {
1579 mutex_unlock(&info->mutex);
1580 return;
1581 }
1582
1583 if (info->gd)
1584 bdev = bdget_disk(info->gd, 0);
1585
1586 mutex_unlock(&info->mutex);
1587
1588 if (!bdev) {
1589 xenbus_frontend_closed(xbdev);
1590 return;
1591 }
1592
1593 mutex_lock(&bdev->bd_mutex);
1594
Daniel Stodden7b32d102010-04-30 22:01:23 +00001595 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001596 xenbus_dev_error(xbdev, -EBUSY,
1597 "Device in use; refusing to close");
1598 xenbus_switch_state(xbdev, XenbusStateClosing);
1599 } else {
1600 xlvbd_release_gendisk(info);
1601 xenbus_frontend_closed(xbdev);
1602 }
1603
1604 mutex_unlock(&bdev->bd_mutex);
1605 bdput(bdev);
1606}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001607
Li Dongyanged30bf32011-09-01 18:39:09 +08001608static void blkfront_setup_discard(struct blkfront_info *info)
1609{
1610 int err;
1611 char *type;
1612 unsigned int discard_granularity;
1613 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001614 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001615
1616 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1617 if (IS_ERR(type))
1618 return;
1619
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001620 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001621 if (strncmp(type, "phy", 3) == 0) {
1622 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1623 "discard-granularity", "%u", &discard_granularity,
1624 "discard-alignment", "%u", &discard_alignment,
1625 NULL);
1626 if (!err) {
1627 info->feature_discard = 1;
1628 info->discard_granularity = discard_granularity;
1629 info->discard_alignment = discard_alignment;
1630 }
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001631 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1632 "discard-secure", "%d", &discard_secure,
1633 NULL);
1634 if (!err)
1635 info->feature_secdiscard = discard_secure;
1636
Li Dongyanged30bf32011-09-01 18:39:09 +08001637 } else if (strncmp(type, "file", 4) == 0)
1638 info->feature_discard = 1;
1639
1640 kfree(type);
1641}
1642
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001643static int blkfront_setup_indirect(struct blkfront_info *info)
1644{
1645 unsigned int indirect_segments, segs;
1646 int err, i;
1647
1648 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1649 "feature-max-indirect-segments", "%u", &indirect_segments,
1650 NULL);
1651 if (err) {
1652 info->max_indirect_segments = 0;
1653 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1654 } else {
1655 info->max_indirect_segments = min(indirect_segments,
1656 xen_blkif_max_segments);
1657 segs = info->max_indirect_segments;
1658 }
1659 info->sg = kzalloc(sizeof(info->sg[0]) * segs, GFP_KERNEL);
1660 if (info->sg == NULL)
1661 goto out_of_memory;
1662 sg_init_table(info->sg, segs);
1663
1664 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
1665 if (err)
1666 goto out_of_memory;
1667
1668 for (i = 0; i < BLK_RING_SIZE; i++) {
1669 info->shadow[i].grants_used = kzalloc(
1670 sizeof(info->shadow[i].grants_used[0]) * segs,
1671 GFP_NOIO);
1672 if (info->max_indirect_segments)
1673 info->shadow[i].indirect_grants = kzalloc(
1674 sizeof(info->shadow[i].indirect_grants[0]) *
1675 INDIRECT_GREFS(segs),
1676 GFP_NOIO);
1677 if ((info->shadow[i].grants_used == NULL) ||
1678 (info->max_indirect_segments &&
1679 (info->shadow[i].indirect_grants == NULL)))
1680 goto out_of_memory;
1681 }
1682
1683
1684 return 0;
1685
1686out_of_memory:
1687 kfree(info->sg);
1688 info->sg = NULL;
1689 for (i = 0; i < BLK_RING_SIZE; i++) {
1690 kfree(info->shadow[i].grants_used);
1691 info->shadow[i].grants_used = NULL;
1692 kfree(info->shadow[i].indirect_grants);
1693 info->shadow[i].indirect_grants = NULL;
1694 }
1695 return -ENOMEM;
1696}
1697
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001698/*
1699 * Invoked when the backend is finally 'ready' (and has told produced
1700 * the details about the physical device - #sectors, size, etc).
1701 */
1702static void blkfront_connect(struct blkfront_info *info)
1703{
1704 unsigned long long sectors;
1705 unsigned long sector_size;
1706 unsigned int binfo;
1707 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001708 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001709
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001710 switch (info->connected) {
1711 case BLKIF_STATE_CONNECTED:
1712 /*
1713 * Potentially, the back-end may be signalling
1714 * a capacity change; update the capacity.
1715 */
1716 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1717 "sectors", "%Lu", &sectors);
1718 if (XENBUS_EXIST_ERR(err))
1719 return;
1720 printk(KERN_INFO "Setting capacity to %Lu\n",
1721 sectors);
1722 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001723 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001724
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001725 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001726 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001727 /*
1728 * If we are recovering from suspension, we need to wait
1729 * for the backend to announce it's features before
1730 * reconnecting, at least we need to know if the backend
1731 * supports indirect descriptors, and how many.
1732 */
1733 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001734 return;
1735
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001736 default:
1737 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001738 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001739
1740 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1741 __func__, info->xbdev->otherend);
1742
1743 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1744 "sectors", "%llu", &sectors,
1745 "info", "%u", &binfo,
1746 "sector-size", "%lu", &sector_size,
1747 NULL);
1748 if (err) {
1749 xenbus_dev_fatal(info->xbdev, err,
1750 "reading backend fields at %s",
1751 info->xbdev->otherend);
1752 return;
1753 }
1754
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001755 info->feature_flush = 0;
1756 info->flush_op = 0;
1757
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001758 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001759 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001760 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001761
1762 /*
1763 * If there's no "feature-barrier" defined, then it means
1764 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001765 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001766 *
Tejun Heo6958f142010-09-03 11:56:16 +02001767 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001768 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001769 if (!err && barrier) {
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001770 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001771 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1772 }
1773 /*
1774 * And if there is "feature-flush-cache" use that above
1775 * barriers.
1776 */
1777 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1778 "feature-flush-cache", "%d", &flush,
1779 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001780
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001781 if (!err && flush) {
1782 info->feature_flush = REQ_FLUSH;
1783 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1784 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001785
1786 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1787 "feature-discard", "%d", &discard,
1788 NULL);
1789
1790 if (!err && discard)
1791 blkfront_setup_discard(info);
1792
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001793 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1794 "feature-persistent", "%u", &persistent,
1795 NULL);
1796 if (err)
1797 info->feature_persistent = 0;
1798 else
1799 info->feature_persistent = persistent;
1800
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001801 err = blkfront_setup_indirect(info);
1802 if (err) {
1803 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1804 info->xbdev->otherend);
1805 return;
1806 }
1807
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001808 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001809 if (err) {
1810 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1811 info->xbdev->otherend);
1812 return;
1813 }
1814
1815 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1816
1817 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001818 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001819 info->connected = BLKIF_STATE_CONNECTED;
1820 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001821 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001822
1823 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001824
1825 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001826}
1827
1828/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001829 * Callback received when the backend's state changes.
1830 */
Ian Campbell203fd612009-12-04 15:33:54 +00001831static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001832 enum xenbus_state backend_state)
1833{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001834 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001835
Ian Campbell203fd612009-12-04 15:33:54 +00001836 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001837
1838 switch (backend_state) {
1839 case XenbusStateInitialising:
1840 case XenbusStateInitWait:
1841 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001842 case XenbusStateReconfiguring:
1843 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001844 case XenbusStateUnknown:
1845 case XenbusStateClosed:
1846 break;
1847
1848 case XenbusStateConnected:
1849 blkfront_connect(info);
1850 break;
1851
1852 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001853 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001854 break;
1855 }
1856}
1857
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001858static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001859{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001860 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1861 struct block_device *bdev = NULL;
1862 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001863
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001864 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001865
1866 blkif_free(info, 0);
1867
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001868 mutex_lock(&info->mutex);
1869
1870 disk = info->gd;
1871 if (disk)
1872 bdev = bdget_disk(disk, 0);
1873
1874 info->xbdev = NULL;
1875 mutex_unlock(&info->mutex);
1876
1877 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001878 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001879 return 0;
1880 }
1881
1882 /*
1883 * The xbdev was removed before we reached the Closed
1884 * state. See if it's safe to remove the disk. If the bdev
1885 * isn't closed yet, we let release take care of it.
1886 */
1887
1888 mutex_lock(&bdev->bd_mutex);
1889 info = disk->private_data;
1890
Daniel Stoddend54142c2010-08-07 18:51:21 +02001891 dev_warn(disk_to_dev(disk),
1892 "%s was hot-unplugged, %d stale handles\n",
1893 xbdev->nodename, bdev->bd_openers);
1894
Daniel Stodden7b32d102010-04-30 22:01:23 +00001895 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001896 xlvbd_release_gendisk(info);
1897 disk->private_data = NULL;
1898 kfree(info);
1899 }
1900
1901 mutex_unlock(&bdev->bd_mutex);
1902 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001903
1904 return 0;
1905}
1906
Christian Limpach1d78d702008-04-02 10:54:04 -07001907static int blkfront_is_ready(struct xenbus_device *dev)
1908{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001909 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001910
Jan Beulich5d7ed202010-08-07 18:31:12 +02001911 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001912}
1913
Al Viroa63c8482008-03-02 10:23:47 -05001914static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001915{
Daniel Stodden13961742010-08-07 18:36:53 +02001916 struct gendisk *disk = bdev->bd_disk;
1917 struct blkfront_info *info;
1918 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001919
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001920 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001921
Daniel Stodden13961742010-08-07 18:36:53 +02001922 info = disk->private_data;
1923 if (!info) {
1924 /* xbdev gone */
1925 err = -ERESTARTSYS;
1926 goto out;
1927 }
1928
1929 mutex_lock(&info->mutex);
1930
1931 if (!info->gd)
1932 /* xbdev is closed */
1933 err = -ERESTARTSYS;
1934
1935 mutex_unlock(&info->mutex);
1936
Daniel Stodden13961742010-08-07 18:36:53 +02001937out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001938 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001939 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001940}
1941
Al Viroa63c8482008-03-02 10:23:47 -05001942static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001943{
Al Viroa63c8482008-03-02 10:23:47 -05001944 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001945 struct block_device *bdev;
1946 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001947
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001948 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001949
1950 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001951
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001952 if (bdev->bd_openers)
1953 goto out;
1954
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001955 /*
1956 * Check if we have been instructed to close. We will have
1957 * deferred this request, because the bdev was still open.
1958 */
1959
1960 mutex_lock(&info->mutex);
1961 xbdev = info->xbdev;
1962
1963 if (xbdev && xbdev->state == XenbusStateClosing) {
1964 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001965 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001966 xlvbd_release_gendisk(info);
1967 xenbus_frontend_closed(info->xbdev);
1968 }
1969
1970 mutex_unlock(&info->mutex);
1971
1972 if (!xbdev) {
1973 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001974 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001975 xlvbd_release_gendisk(info);
1976 disk->private_data = NULL;
1977 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001978 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001979
Jens Axboea4cc14e2010-08-08 21:50:05 -04001980out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01001981 bdput(bdev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001982 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001983 return 0;
1984}
1985
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001986static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001987{
1988 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001989 .open = blkif_open,
1990 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001991 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001992 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001993};
1994
1995
Márton Némethec9c42e2010-01-10 13:39:52 +01001996static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001997 { "vbd" },
1998 { "" }
1999};
2000
Jan Beulich73db1442011-12-22 09:08:13 +00002001static DEFINE_XENBUS_DRIVER(blkfront, ,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002002 .probe = blkfront_probe,
2003 .remove = blkfront_remove,
2004 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002005 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002006 .is_ready = blkfront_is_ready,
Jan Beulich73db1442011-12-22 09:08:13 +00002007);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002008
2009static int __init xlblk_init(void)
2010{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002011 int ret;
2012
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002013 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002014 return -ENODEV;
2015
Igor Mammedove95ae5a2012-03-27 19:31:08 +02002016 if (xen_hvm_domain() && !xen_platform_pci_unplug)
Igor Mammedovb9136d22012-03-21 15:08:38 +01002017 return -ENODEV;
2018
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002019 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2020 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2021 XENVBD_MAJOR, DEV_NAME);
2022 return -ENODEV;
2023 }
2024
Jan Beulich73db1442011-12-22 09:08:13 +00002025 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002026 if (ret) {
2027 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2028 return ret;
2029 }
2030
2031 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002032}
2033module_init(xlblk_init);
2034
2035
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002036static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002037{
Jan Beulich86050672012-04-05 16:04:52 +01002038 xenbus_unregister_driver(&blkfront_driver);
2039 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2040 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002041}
2042module_exit(xlblk_exit);
2043
2044MODULE_DESCRIPTION("Xen virtual block device frontend");
2045MODULE_LICENSE("GPL");
2046MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002047MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002048MODULE_ALIAS("xenblk");