blob: 88e23fd8c7f3d60a34d45d99a306c43074ae445d [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;
Konrad Rzeszutek Wilk2d5dc3b2013-05-15 10:39:34 -040098module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
99MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200100
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -0800101#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700102
103/*
104 * We have one of these per vbd, whether ide, scsi or 'other'. They
105 * hang in private_data off the gendisk structure. We may end up
106 * putting all kinds of interesting stuff here :-)
107 */
108struct blkfront_info
109{
Steven Noonan34678112012-02-17 12:04:44 -0800110 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000111 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700112 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700113 struct gendisk *gd;
114 int vdevice;
115 blkif_vdev_t handle;
116 enum blkif_state connected;
117 int ring_ref;
118 struct blkif_front_ring ring;
119 unsigned int evtchn, irq;
120 struct request_queue *rq;
121 struct work_struct work;
122 struct gnttab_free_callback callback;
123 struct blk_shadow shadow[BLK_RING_SIZE];
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100124 struct list_head grants;
125 struct list_head indirect_pages;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200126 unsigned int persistent_gnts_c;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700127 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200128 unsigned int feature_flush;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400129 unsigned int feature_discard:1;
130 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800131 unsigned int discard_granularity;
132 unsigned int discard_alignment;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200133 unsigned int feature_persistent:1;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200134 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700135 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700136};
137
Jan Beulich0e345822010-08-07 18:28:55 +0200138static unsigned int nr_minors;
139static unsigned long *minors;
140static DEFINE_SPINLOCK(minor_lock);
141
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700142#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
143 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
144#define GRANT_INVALID_REF 0
145
146#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700147#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700148
149#define BLKIF_MAJOR(dev) ((dev)>>8)
150#define BLKIF_MINOR(dev) ((dev) & 0xff)
151
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700152#define EXT_SHIFT 28
153#define EXTENDED (1<<EXT_SHIFT)
154#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
155#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000156#define EMULATED_HD_DISK_MINOR_OFFSET (0)
157#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200158#define EMULATED_SD_DISK_MINOR_OFFSET (0)
159#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700160
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700161#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700162
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200163#define SEGS_PER_INDIRECT_FRAME \
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100164 (PAGE_SIZE/sizeof(struct blkif_request_segment))
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200165#define INDIRECT_GREFS(_segs) \
166 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
167
168static int blkfront_setup_indirect(struct blkfront_info *info);
169
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700170static int get_id_from_freelist(struct blkfront_info *info)
171{
172 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200173 BUG_ON(free >= BLK_RING_SIZE);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400174 info->shadow_free = info->shadow[free].req.u.rw.id;
175 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700176 return free;
177}
178
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400179static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700180 unsigned long id)
181{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400182 if (info->shadow[id].req.u.rw.id != id)
183 return -EINVAL;
184 if (info->shadow[id].request == NULL)
185 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400186 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400187 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700188 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400189 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700190}
191
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100192static int fill_grant_buffer(struct blkfront_info *info, int num)
193{
194 struct page *granted_page;
195 struct grant *gnt_list_entry, *n;
196 int i = 0;
197
198 while(i < num) {
199 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
200 if (!gnt_list_entry)
201 goto out_of_memory;
202
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100203 if (info->feature_persistent) {
204 granted_page = alloc_page(GFP_NOIO);
205 if (!granted_page) {
206 kfree(gnt_list_entry);
207 goto out_of_memory;
208 }
209 gnt_list_entry->pfn = page_to_pfn(granted_page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100210 }
211
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100212 gnt_list_entry->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100213 list_add(&gnt_list_entry->node, &info->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100214 i++;
215 }
216
217 return 0;
218
219out_of_memory:
220 list_for_each_entry_safe(gnt_list_entry, n,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100221 &info->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100222 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100223 if (info->feature_persistent)
224 __free_page(pfn_to_page(gnt_list_entry->pfn));
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100225 kfree(gnt_list_entry);
226 i--;
227 }
228 BUG_ON(i != 0);
229 return -ENOMEM;
230}
231
232static struct grant *get_grant(grant_ref_t *gref_head,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100233 unsigned long pfn,
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100234 struct blkfront_info *info)
235{
236 struct grant *gnt_list_entry;
237 unsigned long buffer_mfn;
238
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100239 BUG_ON(list_empty(&info->grants));
240 gnt_list_entry = list_first_entry(&info->grants, struct grant,
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100241 node);
242 list_del(&gnt_list_entry->node);
243
244 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
245 info->persistent_gnts_c--;
246 return gnt_list_entry;
247 }
248
249 /* Assign a gref to this page */
250 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
251 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100252 if (!info->feature_persistent) {
253 BUG_ON(!pfn);
254 gnt_list_entry->pfn = pfn;
255 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100256 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
257 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
258 info->xbdev->otherend_id,
259 buffer_mfn, 0);
260 return gnt_list_entry;
261}
262
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400263static const char *op_name(int op)
264{
265 static const char *const names[] = {
266 [BLKIF_OP_READ] = "read",
267 [BLKIF_OP_WRITE] = "write",
268 [BLKIF_OP_WRITE_BARRIER] = "barrier",
269 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
270 [BLKIF_OP_DISCARD] = "discard" };
271
272 if (op < 0 || op >= ARRAY_SIZE(names))
273 return "unknown";
274
275 if (!names[op])
276 return "reserved";
277
278 return names[op];
279}
Jan Beulich0e345822010-08-07 18:28:55 +0200280static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
281{
282 unsigned int end = minor + nr;
283 int rc;
284
285 if (end > nr_minors) {
286 unsigned long *bitmap, *old;
287
Thomas Meyerf0941482011-11-29 22:08:00 +0100288 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200289 GFP_KERNEL);
290 if (bitmap == NULL)
291 return -ENOMEM;
292
293 spin_lock(&minor_lock);
294 if (end > nr_minors) {
295 old = minors;
296 memcpy(bitmap, minors,
297 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
298 minors = bitmap;
299 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
300 } else
301 old = bitmap;
302 spin_unlock(&minor_lock);
303 kfree(old);
304 }
305
306 spin_lock(&minor_lock);
307 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900308 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200309 rc = 0;
310 } else
311 rc = -EBUSY;
312 spin_unlock(&minor_lock);
313
314 return rc;
315}
316
317static void xlbd_release_minors(unsigned int minor, unsigned int nr)
318{
319 unsigned int end = minor + nr;
320
321 BUG_ON(end > nr_minors);
322 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900323 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200324 spin_unlock(&minor_lock);
325}
326
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700327static void blkif_restart_queue_callback(void *arg)
328{
329 struct blkfront_info *info = (struct blkfront_info *)arg;
330 schedule_work(&info->work);
331}
332
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700333static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800334{
335 /* We don't have real geometry info, but let's at least return
336 values consistent with the size of the device */
337 sector_t nsect = get_capacity(bd->bd_disk);
338 sector_t cylinders = nsect;
339
340 hg->heads = 0xff;
341 hg->sectors = 0x3f;
342 sector_div(cylinders, hg->heads * hg->sectors);
343 hg->cylinders = cylinders;
344 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
345 hg->cylinders = 0xffff;
346 return 0;
347}
348
Al Viroa63c8482008-03-02 10:23:47 -0500349static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200350 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200351{
Al Viroa63c8482008-03-02 10:23:47 -0500352 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200353 int i;
354
355 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
356 command, (long)argument);
357
358 switch (command) {
359 case CDROMMULTISESSION:
360 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
361 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
362 if (put_user(0, (char __user *)(argument + i)))
363 return -EFAULT;
364 return 0;
365
366 case CDROM_GET_CAPABILITY: {
367 struct gendisk *gd = info->gd;
368 if (gd->flags & GENHD_FL_CD)
369 return 0;
370 return -EINVAL;
371 }
372
373 default:
374 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
375 command);*/
376 return -EINVAL; /* same return as native Linux */
377 }
378
379 return 0;
380}
381
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700382/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400383 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400384 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700385 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400386 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700387 */
388static int blkif_queue_request(struct request *req)
389{
390 struct blkfront_info *info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700391 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700392 unsigned long id;
393 unsigned int fsect, lsect;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200394 int i, ref, n;
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100395 struct blkif_request_segment *segments = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200396
397 /*
398 * Used to store if we are able to queue the request by just using
399 * existing persistent grants, or if we have to get new grants,
400 * as there are not sufficiently many free.
401 */
402 bool new_persistent_gnts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700403 grant_ref_t gref_head;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200404 struct grant *gnt_list_entry = NULL;
Jens Axboe9e973e62009-02-24 08:10:09 +0100405 struct scatterlist *sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200406 int nseg, max_grefs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700407
408 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
409 return 1;
410
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200411 max_grefs = req->nr_phys_segments;
412 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
413 /*
414 * If we are using indirect segments we need to account
415 * for the indirect grefs used in the request.
416 */
417 max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200418
419 /* Check if we have enough grants to allocate a requests */
420 if (info->persistent_gnts_c < max_grefs) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200421 new_persistent_gnts = 1;
422 if (gnttab_alloc_grant_references(
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200423 max_grefs - info->persistent_gnts_c,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200424 &gref_head) < 0) {
425 gnttab_request_free_callback(
426 &info->callback,
427 blkif_restart_queue_callback,
428 info,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200429 max_grefs);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200430 return 1;
431 }
432 } else
433 new_persistent_gnts = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700434
435 /* Fill out a communications ring structure. */
436 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
437 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400438 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700439
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400440 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800441 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800442 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200443 ring_req->u.discard.id = id;
444 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400445 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
446 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
447 else
448 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800449 } else {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200450 BUG_ON(info->max_indirect_segments == 0 &&
451 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
452 BUG_ON(info->max_indirect_segments &&
453 req->nr_phys_segments > info->max_indirect_segments);
Roger Pau Monneb7649152013-05-02 10:58:50 +0200454 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200455 ring_req->u.rw.id = id;
456 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
457 /*
458 * The indirect operation can only be a BLKIF_OP_READ or
459 * BLKIF_OP_WRITE
460 */
461 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
462 ring_req->operation = BLKIF_OP_INDIRECT;
463 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
464 BLKIF_OP_WRITE : BLKIF_OP_READ;
465 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
466 ring_req->u.indirect.handle = info->handle;
467 ring_req->u.indirect.nr_segments = nseg;
468 } else {
469 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
470 ring_req->u.rw.handle = info->handle;
471 ring_req->operation = rq_data_dir(req) ?
472 BLKIF_OP_WRITE : BLKIF_OP_READ;
473 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
474 /*
475 * Ideally we can do an unordered flush-to-disk. In case the
476 * backend onlysupports barriers, use that. A barrier request
477 * a superset of FUA, so we can implement it the same
478 * way. (It's also a FLUSH+FUA, since it is
479 * guaranteed ordered WRT previous writes.)
480 */
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100481 switch (info->feature_flush &
482 ((REQ_FLUSH|REQ_FUA))) {
483 case REQ_FLUSH|REQ_FUA:
484 ring_req->operation =
485 BLKIF_OP_WRITE_BARRIER;
486 break;
487 case REQ_FLUSH:
488 ring_req->operation =
489 BLKIF_OP_FLUSH_DISKCACHE;
490 break;
491 default:
492 ring_req->operation = 0;
493 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200494 }
495 ring_req->u.rw.nr_segments = nseg;
496 }
Roger Pau Monneb7649152013-05-02 10:58:50 +0200497 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800498 fsect = sg->offset >> 9;
499 lsect = fsect + (sg->length >> 9) - 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700500
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200501 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
502 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
Tim Gardner427bfe02013-11-14 14:29:52 -0700503 unsigned long uninitialized_var(pfn);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100504
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200505 if (segments)
506 kunmap_atomic(segments);
507
508 n = i / SEGS_PER_INDIRECT_FRAME;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100509 if (!info->feature_persistent) {
510 struct page *indirect_page;
511
512 /* Fetch a pre-allocated page to use for indirect grefs */
513 BUG_ON(list_empty(&info->indirect_pages));
514 indirect_page = list_first_entry(&info->indirect_pages,
515 struct page, lru);
516 list_del(&indirect_page->lru);
517 pfn = page_to_pfn(indirect_page);
518 }
519 gnt_list_entry = get_grant(&gref_head, pfn, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200520 info->shadow[id].indirect_grants[n] = gnt_list_entry;
521 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
522 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
523 }
524
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100525 gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100526 ref = gnt_list_entry->gref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200527
528 info->shadow[id].grants_used[i] = gnt_list_entry;
529
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100530 if (rq_data_dir(req) && info->feature_persistent) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200531 char *bvec_data;
532 void *shared_data;
533
534 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
535
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200536 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200537 bvec_data = kmap_atomic(sg_page(sg));
538
539 /*
540 * this does not wipe data stored outside the
541 * range sg->offset..sg->offset+sg->length.
542 * Therefore, blkback *could* see data from
543 * previous requests. This is OK as long as
544 * persistent grants are shared with just one
545 * domain. It may need refactoring if this
546 * changes
547 */
548 memcpy(shared_data + sg->offset,
549 bvec_data + sg->offset,
550 sg->length);
551
552 kunmap_atomic(bvec_data);
553 kunmap_atomic(shared_data);
554 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200555 if (ring_req->operation != BLKIF_OP_INDIRECT) {
556 ring_req->u.rw.seg[i] =
557 (struct blkif_request_segment) {
558 .gref = ref,
559 .first_sect = fsect,
560 .last_sect = lsect };
561 } else {
562 n = i % SEGS_PER_INDIRECT_FRAME;
563 segments[n] =
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100564 (struct blkif_request_segment) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200565 .gref = ref,
566 .first_sect = fsect,
567 .last_sect = lsect };
568 }
Li Dongyanged30bf32011-09-01 18:39:09 +0800569 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200570 if (segments)
571 kunmap_atomic(segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700572 }
573
574 info->ring.req_prod_pvt++;
575
576 /* Keep a private copy so we can reissue requests when recovering. */
577 info->shadow[id].req = *ring_req;
578
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200579 if (new_persistent_gnts)
580 gnttab_free_grant_references(gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700581
582 return 0;
583}
584
585
586static inline void flush_requests(struct blkfront_info *info)
587{
588 int notify;
589
590 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
591
592 if (notify)
593 notify_remote_via_irq(info->irq);
594}
595
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100596static inline bool blkif_request_flush_invalid(struct request *req,
597 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200598{
599 return ((req->cmd_type != REQ_TYPE_FS) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100600 ((req->cmd_flags & REQ_FLUSH) &&
601 !(info->feature_flush & REQ_FLUSH)) ||
602 ((req->cmd_flags & REQ_FUA) &&
603 !(info->feature_flush & REQ_FUA)));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200604}
605
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700606/*
607 * do_blkif_request
608 * read a block; request is in a request queue
609 */
Jens Axboe165125e2007-07-24 09:28:11 +0200610static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700611{
612 struct blkfront_info *info = NULL;
613 struct request *req;
614 int queued;
615
616 pr_debug("Entered do_blkif_request\n");
617
618 queued = 0;
619
Tejun Heo9934c8c2009-05-08 11:54:16 +0900620 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700621 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700622
623 if (RING_FULL(&info->ring))
624 goto wait;
625
Tejun Heo9934c8c2009-05-08 11:54:16 +0900626 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900627
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100628 if (blkif_request_flush_invalid(req, info)) {
629 __blk_end_request_all(req, -EOPNOTSUPP);
Tejun Heo296b2f62009-05-08 11:54:15 +0900630 continue;
631 }
632
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700633 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Jens Axboeb4f42e22014-04-10 09:46:28 -0600634 "(%u/%u) [%s]\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900635 req, req->cmd, (unsigned long)blk_rq_pos(req),
636 blk_rq_cur_sectors(req), blk_rq_sectors(req),
Jens Axboeb4f42e22014-04-10 09:46:28 -0600637 rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700638
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700639 if (blkif_queue_request(req)) {
640 blk_requeue_request(rq, req);
641wait:
642 /* Avoid pointless unplugs. */
643 blk_stop_queue(rq);
644 break;
645 }
646
647 queued++;
648 }
649
650 if (queued != 0)
651 flush_requests(info);
652}
653
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200654static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200655 unsigned int physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200656 unsigned int segments)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700657{
Jens Axboe165125e2007-07-24 09:28:11 +0200658 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800659 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700660
Steven Noonan34678112012-02-17 12:04:44 -0800661 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700662 if (rq == NULL)
663 return -1;
664
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900665 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700666
Li Dongyanged30bf32011-09-01 18:39:09 +0800667 if (info->feature_discard) {
668 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
669 blk_queue_max_discard_sectors(rq, get_capacity(gd));
670 rq->limits.discard_granularity = info->discard_granularity;
671 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400672 if (info->feature_secdiscard)
673 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800674 }
675
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700676 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400677 blk_queue_logical_block_size(rq, sector_size);
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200678 blk_queue_physical_block_size(rq, physical_sector_size);
Roger Pau Monne294caaf2013-06-21 12:56:54 +0200679 blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700680
681 /* Each segment in a request is up to an aligned page in size. */
682 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
683 blk_queue_max_segment_size(rq, PAGE_SIZE);
684
685 /* Ensure a merged request will fit in a single I/O ring slot. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200686 blk_queue_max_segments(rq, segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700687
688 /* Make sure buffer addresses are sector-aligned. */
689 blk_queue_dma_alignment(rq, 511);
690
Ian Campbell1c91fe12008-06-17 10:47:08 +0200691 /* Make sure we don't use bounce buffers. */
692 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
693
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700694 gd->queue = rq;
695
696 return 0;
697}
698
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100699static const char *flush_info(unsigned int feature_flush)
700{
701 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
702 case REQ_FLUSH|REQ_FUA:
703 return "barrier: enabled;";
704 case REQ_FLUSH:
705 return "flush diskcache: enabled;";
706 default:
707 return "barrier or flush: disabled;";
708 }
709}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700710
Tejun Heo4913efe2010-09-03 11:56:16 +0200711static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700712{
Tejun Heo4913efe2010-09-03 11:56:16 +0200713 blk_queue_flush(info->rq, info->feature_flush);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100714 pr_info("blkfront: %s: %s %s %s %s %s\n",
715 info->gd->disk_name, flush_info(info->feature_flush),
716 "persistent grants:", info->feature_persistent ?
717 "enabled;" : "disabled;", "indirect descriptors:",
718 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700719}
720
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000721static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
722{
723 int major;
724 major = BLKIF_MAJOR(vdevice);
725 *minor = BLKIF_MINOR(vdevice);
726 switch (major) {
727 case XEN_IDE0_MAJOR:
728 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
729 *minor = ((*minor / 64) * PARTS_PER_DISK) +
730 EMULATED_HD_DISK_MINOR_OFFSET;
731 break;
732 case XEN_IDE1_MAJOR:
733 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
734 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
735 EMULATED_HD_DISK_MINOR_OFFSET;
736 break;
737 case XEN_SCSI_DISK0_MAJOR:
738 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
739 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
740 break;
741 case XEN_SCSI_DISK1_MAJOR:
742 case XEN_SCSI_DISK2_MAJOR:
743 case XEN_SCSI_DISK3_MAJOR:
744 case XEN_SCSI_DISK4_MAJOR:
745 case XEN_SCSI_DISK5_MAJOR:
746 case XEN_SCSI_DISK6_MAJOR:
747 case XEN_SCSI_DISK7_MAJOR:
748 *offset = (*minor / PARTS_PER_DISK) +
749 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
750 EMULATED_SD_DISK_NAME_OFFSET;
751 *minor = *minor +
752 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
753 EMULATED_SD_DISK_MINOR_OFFSET;
754 break;
755 case XEN_SCSI_DISK8_MAJOR:
756 case XEN_SCSI_DISK9_MAJOR:
757 case XEN_SCSI_DISK10_MAJOR:
758 case XEN_SCSI_DISK11_MAJOR:
759 case XEN_SCSI_DISK12_MAJOR:
760 case XEN_SCSI_DISK13_MAJOR:
761 case XEN_SCSI_DISK14_MAJOR:
762 case XEN_SCSI_DISK15_MAJOR:
763 *offset = (*minor / PARTS_PER_DISK) +
764 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
765 EMULATED_SD_DISK_NAME_OFFSET;
766 *minor = *minor +
767 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
768 EMULATED_SD_DISK_MINOR_OFFSET;
769 break;
770 case XENVBD_MAJOR:
771 *offset = *minor / PARTS_PER_DISK;
772 break;
773 default:
774 printk(KERN_WARNING "blkfront: your disk configuration is "
775 "incorrect, please use an xvd device instead\n");
776 return -ENODEV;
777 }
778 return 0;
779}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700780
Jan Beuliche77c78c2012-04-05 16:37:22 +0100781static char *encode_disk_name(char *ptr, unsigned int n)
782{
783 if (n >= 26)
784 ptr = encode_disk_name(ptr, n / 26 - 1);
785 *ptr = 'a' + n % 26;
786 return ptr + 1;
787}
788
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700789static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
790 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200791 u16 vdisk_info, u16 sector_size,
792 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700793{
794 struct gendisk *gd;
795 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000796 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700797 unsigned int offset;
798 int minor;
799 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100800 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700801
802 BUG_ON(info->gd != NULL);
803 BUG_ON(info->rq != NULL);
804
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700805 if ((info->vdevice>>EXT_SHIFT) > 1) {
806 /* this is above the extended range; something is wrong */
807 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
808 return -ENODEV;
809 }
810
811 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000812 err = xen_translate_vdev(info->vdevice, &minor, &offset);
813 if (err)
814 return err;
815 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700816 } else {
817 minor = BLKIF_MINOR_EXT(info->vdevice);
818 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000819 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200820 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000821 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
822 "emulated IDE disks,\n\t choose an xvd device name"
823 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700824 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100825 if (minor >> MINORBITS) {
826 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
827 info->vdevice, minor);
828 return -ENODEV;
829 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700830
831 if ((minor % nr_parts) == 0)
832 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700833
Jan Beulich0e345822010-08-07 18:28:55 +0200834 err = xlbd_reserve_minors(minor, nr_minors);
835 if (err)
836 goto out;
837 err = -ENODEV;
838
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700839 gd = alloc_disk(nr_minors);
840 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200841 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700842
Jan Beuliche77c78c2012-04-05 16:37:22 +0100843 strcpy(gd->disk_name, DEV_NAME);
844 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
845 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
846 if (nr_minors > 1)
847 *ptr = 0;
848 else
849 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
850 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700851
852 gd->major = XENVBD_MAJOR;
853 gd->first_minor = minor;
854 gd->fops = &xlvbd_block_fops;
855 gd->private_data = info;
856 gd->driverfs_dev = &(info->xbdev->dev);
857 set_capacity(gd, capacity);
858
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200859 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200860 info->max_indirect_segments ? :
861 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700862 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200863 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700864 }
865
866 info->rq = gd->queue;
867 info->gd = gd;
868
Tejun Heo4913efe2010-09-03 11:56:16 +0200869 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700870
871 if (vdisk_info & VDISK_READONLY)
872 set_disk_ro(gd, 1);
873
874 if (vdisk_info & VDISK_REMOVABLE)
875 gd->flags |= GENHD_FL_REMOVABLE;
876
877 if (vdisk_info & VDISK_CDROM)
878 gd->flags |= GENHD_FL_CD;
879
880 return 0;
881
Jan Beulich0e345822010-08-07 18:28:55 +0200882 release:
883 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700884 out:
885 return err;
886}
887
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200888static void xlvbd_release_gendisk(struct blkfront_info *info)
889{
890 unsigned int minor, nr_minors;
891 unsigned long flags;
892
893 if (info->rq == NULL)
894 return;
895
Steven Noonan34678112012-02-17 12:04:44 -0800896 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200897
898 /* No more blkif_request(). */
899 blk_stop_queue(info->rq);
900
901 /* No more gnttab callback work. */
902 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800903 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200904
905 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700906 flush_work(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200907
908 del_gendisk(info->gd);
909
910 minor = info->gd->first_minor;
911 nr_minors = info->gd->minors;
912 xlbd_release_minors(minor, nr_minors);
913
914 blk_cleanup_queue(info->rq);
915 info->rq = NULL;
916
917 put_disk(info->gd);
918 info->gd = NULL;
919}
920
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700921static void kick_pending_request_queues(struct blkfront_info *info)
922{
923 if (!RING_FULL(&info->ring)) {
924 /* Re-enable calldowns. */
925 blk_start_queue(info->rq);
926 /* Kick things off immediately. */
927 do_blkif_request(info->rq);
928 }
929}
930
931static void blkif_restart_queue(struct work_struct *work)
932{
933 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
934
Steven Noonan34678112012-02-17 12:04:44 -0800935 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700936 if (info->connected == BLKIF_STATE_CONNECTED)
937 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800938 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700939}
940
941static void blkif_free(struct blkfront_info *info, int suspend)
942{
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100943 struct grant *persistent_gnt;
944 struct grant *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200945 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200946
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700947 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800948 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700949 info->connected = suspend ?
950 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
951 /* No more blkif_request(). */
952 if (info->rq)
953 blk_stop_queue(info->rq);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200954
955 /* Remove all persistent grants */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100956 if (!list_empty(&info->grants)) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100957 list_for_each_entry_safe(persistent_gnt, n,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100958 &info->grants, node) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100959 list_del(&persistent_gnt->node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100960 if (persistent_gnt->gref != GRANT_INVALID_REF) {
961 gnttab_end_foreign_access(persistent_gnt->gref,
962 0, 0UL);
963 info->persistent_gnts_c--;
964 }
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100965 if (info->feature_persistent)
966 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100967 kfree(persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200968 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200969 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100970 BUG_ON(info->persistent_gnts_c != 0);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200971
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100972 /*
973 * Remove indirect pages, this only happens when using indirect
974 * descriptors but not persistent grants
975 */
976 if (!list_empty(&info->indirect_pages)) {
977 struct page *indirect_page, *n;
978
979 BUG_ON(info->feature_persistent);
980 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
981 list_del(&indirect_page->lru);
982 __free_page(indirect_page);
983 }
984 }
985
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200986 for (i = 0; i < BLK_RING_SIZE; i++) {
987 /*
988 * Clear persistent grants present in requests already
989 * on the shared ring
990 */
991 if (!info->shadow[i].request)
992 goto free_shadow;
993
994 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
995 info->shadow[i].req.u.indirect.nr_segments :
996 info->shadow[i].req.u.rw.nr_segments;
997 for (j = 0; j < segs; j++) {
998 persistent_gnt = info->shadow[i].grants_used[j];
999 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001000 if (info->feature_persistent)
1001 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001002 kfree(persistent_gnt);
1003 }
1004
1005 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1006 /*
1007 * If this is not an indirect operation don't try to
1008 * free indirect segments
1009 */
1010 goto free_shadow;
1011
1012 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1013 persistent_gnt = info->shadow[i].indirect_grants[j];
1014 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1015 __free_page(pfn_to_page(persistent_gnt->pfn));
1016 kfree(persistent_gnt);
1017 }
1018
1019free_shadow:
1020 kfree(info->shadow[i].grants_used);
1021 info->shadow[i].grants_used = NULL;
1022 kfree(info->shadow[i].indirect_grants);
1023 info->shadow[i].indirect_grants = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001024 kfree(info->shadow[i].sg);
1025 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001026 }
1027
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001028 /* No more gnttab callback work. */
1029 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -08001030 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001031
1032 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -07001033 flush_work(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001034
1035 /* Free resources associated with old device channel. */
1036 if (info->ring_ref != GRANT_INVALID_REF) {
1037 gnttab_end_foreign_access(info->ring_ref, 0,
1038 (unsigned long)info->ring.sring);
1039 info->ring_ref = GRANT_INVALID_REF;
1040 info->ring.sring = NULL;
1041 }
1042 if (info->irq)
1043 unbind_from_irqhandler(info->irq, info);
1044 info->evtchn = info->irq = 0;
1045
1046}
1047
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001048static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1049 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001050{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001051 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001052 struct scatterlist *sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001053 char *bvec_data;
1054 void *shared_data;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001055 int nseg;
1056
1057 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
1058 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001059
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001060 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001061 /*
1062 * Copy the data received from the backend into the bvec.
1063 * Since bv_offset can be different than 0, and bv_len different
1064 * than PAGE_SIZE, we have to keep track of the current offset,
1065 * to be sure we are copying the data from the right shared page.
1066 */
Roger Pau Monneb7649152013-05-02 10:58:50 +02001067 for_each_sg(s->sg, sg, nseg, i) {
1068 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001069 shared_data = kmap_atomic(
1070 pfn_to_page(s->grants_used[i]->pfn));
Roger Pau Monneb7649152013-05-02 10:58:50 +02001071 bvec_data = kmap_atomic(sg_page(sg));
1072 memcpy(bvec_data + sg->offset,
1073 shared_data + sg->offset,
1074 sg->length);
1075 kunmap_atomic(bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001076 kunmap_atomic(shared_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001077 }
1078 }
1079 /* Add the persistent grant into the list of free grants */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001080 for (i = 0; i < nseg; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001081 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1082 /*
1083 * If the grant is still mapped by the backend (the
1084 * backend has chosen to make this grant persistent)
1085 * we add it at the head of the list, so it will be
1086 * reused first.
1087 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001088 if (!info->feature_persistent)
1089 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1090 s->grants_used[i]->gref);
1091 list_add(&s->grants_used[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001092 info->persistent_gnts_c++;
1093 } else {
1094 /*
1095 * If the grant is not mapped by the backend we end the
1096 * foreign access and add it to the tail of the list,
1097 * so it will not be picked again unless we run out of
1098 * persistent grants.
1099 */
1100 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1101 s->grants_used[i]->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001102 list_add_tail(&s->grants_used[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001103 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001104 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001105 if (s->req.operation == BLKIF_OP_INDIRECT) {
1106 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001107 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001108 if (!info->feature_persistent)
1109 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1110 s->indirect_grants[i]->gref);
1111 list_add(&s->indirect_grants[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001112 info->persistent_gnts_c++;
1113 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001114 struct page *indirect_page;
1115
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001116 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001117 /*
1118 * Add the used indirect page back to the list of
1119 * available pages for indirect grefs.
1120 */
1121 indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
1122 list_add(&indirect_page->lru, &info->indirect_pages);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001123 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001124 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001125 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001126 }
1127 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001128}
1129
1130static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1131{
1132 struct request *req;
1133 struct blkif_response *bret;
1134 RING_IDX i, rp;
1135 unsigned long flags;
1136 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001137 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001138
Steven Noonan34678112012-02-17 12:04:44 -08001139 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001140
1141 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -08001142 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001143 return IRQ_HANDLED;
1144 }
1145
1146 again:
1147 rp = info->ring.sring->rsp_prod;
1148 rmb(); /* Ensure we see queued responses up to 'rp'. */
1149
1150 for (i = info->ring.rsp_cons; i != rp; i++) {
1151 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001152
1153 bret = RING_GET_RESPONSE(&info->ring, i);
1154 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001155 /*
1156 * The backend has messed up and given us an id that we would
1157 * never have given to it (we stamp it up to BLK_RING_SIZE -
1158 * look in get_id_from_freelist.
1159 */
1160 if (id >= BLK_RING_SIZE) {
1161 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1162 info->gd->disk_name, op_name(bret->operation), id);
1163 /* We can't safely get the 'struct request' as
1164 * the id is busted. */
1165 continue;
1166 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001167 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001168
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001169 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001170 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001171
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001172 if (add_id_to_freelist(info, id)) {
1173 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1174 info->gd->disk_name, op_name(bret->operation), id);
1175 continue;
1176 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001177
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001178 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001179 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001180 case BLKIF_OP_DISCARD:
1181 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1182 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001183 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1184 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +08001185 error = -EOPNOTSUPP;
1186 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001187 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001188 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001189 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001190 }
1191 __blk_end_request_all(req, error);
1192 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001193 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001194 case BLKIF_OP_WRITE_BARRIER:
1195 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001196 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1197 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001198 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001199 }
1200 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001201 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001202 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1203 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001204 error = -EOPNOTSUPP;
1205 }
1206 if (unlikely(error)) {
1207 if (error == -EOPNOTSUPP)
1208 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001209 info->feature_flush = 0;
1210 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001211 }
1212 /* fall through */
1213 case BLKIF_OP_READ:
1214 case BLKIF_OP_WRITE:
1215 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1216 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1217 "request: %x\n", bret->status);
1218
Tejun Heo40cbbb72009-04-23 11:05:19 +09001219 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001220 break;
1221 default:
1222 BUG();
1223 }
1224 }
1225
1226 info->ring.rsp_cons = i;
1227
1228 if (i != info->ring.req_prod_pvt) {
1229 int more_to_do;
1230 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1231 if (more_to_do)
1232 goto again;
1233 } else
1234 info->ring.sring->rsp_event = i + 1;
1235
1236 kick_pending_request_queues(info);
1237
Steven Noonan34678112012-02-17 12:04:44 -08001238 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001239
1240 return IRQ_HANDLED;
1241}
1242
1243
1244static int setup_blkring(struct xenbus_device *dev,
1245 struct blkfront_info *info)
1246{
1247 struct blkif_sring *sring;
Wei Liuccc9d902015-04-03 14:44:59 +08001248 grant_ref_t gref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001249 int err;
1250
1251 info->ring_ref = GRANT_INVALID_REF;
1252
Ian Campbella144ff02008-06-17 10:47:08 +02001253 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001254 if (!sring) {
1255 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1256 return -ENOMEM;
1257 }
1258 SHARED_RING_INIT(sring);
1259 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1260
Wei Liuccc9d902015-04-03 14:44:59 +08001261 err = xenbus_grant_ring(dev, info->ring.sring, 1, &gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001262 if (err < 0) {
1263 free_page((unsigned long)sring);
1264 info->ring.sring = NULL;
1265 goto fail;
1266 }
Wei Liuccc9d902015-04-03 14:44:59 +08001267 info->ring_ref = gref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001268
1269 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1270 if (err)
1271 goto fail;
1272
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001273 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1274 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001275 if (err <= 0) {
1276 xenbus_dev_fatal(dev, err,
1277 "bind_evtchn_to_irqhandler failed");
1278 goto fail;
1279 }
1280 info->irq = err;
1281
1282 return 0;
1283fail:
1284 blkif_free(info, 0);
1285 return err;
1286}
1287
1288
1289/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001290static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001291 struct blkfront_info *info)
1292{
1293 const char *message = NULL;
1294 struct xenbus_transaction xbt;
1295 int err;
1296
1297 /* Create shared ring, alloc event channel. */
1298 err = setup_blkring(dev, info);
1299 if (err)
1300 goto out;
1301
1302again:
1303 err = xenbus_transaction_start(&xbt);
1304 if (err) {
1305 xenbus_dev_fatal(dev, err, "starting transaction");
1306 goto destroy_blkring;
1307 }
1308
1309 err = xenbus_printf(xbt, dev->nodename,
1310 "ring-ref", "%u", info->ring_ref);
1311 if (err) {
1312 message = "writing ring-ref";
1313 goto abort_transaction;
1314 }
1315 err = xenbus_printf(xbt, dev->nodename,
1316 "event-channel", "%u", info->evtchn);
1317 if (err) {
1318 message = "writing event-channel";
1319 goto abort_transaction;
1320 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001321 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1322 XEN_IO_PROTO_ABI_NATIVE);
1323 if (err) {
1324 message = "writing protocol";
1325 goto abort_transaction;
1326 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001327 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001328 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001329 if (err)
1330 dev_warn(&dev->dev,
1331 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001332
1333 err = xenbus_transaction_end(xbt, 0);
1334 if (err) {
1335 if (err == -EAGAIN)
1336 goto again;
1337 xenbus_dev_fatal(dev, err, "completing transaction");
1338 goto destroy_blkring;
1339 }
1340
1341 xenbus_switch_state(dev, XenbusStateInitialised);
1342
1343 return 0;
1344
1345 abort_transaction:
1346 xenbus_transaction_end(xbt, 1);
1347 if (message)
1348 xenbus_dev_fatal(dev, err, "%s", message);
1349 destroy_blkring:
1350 blkif_free(info, 0);
1351 out:
1352 return err;
1353}
1354
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001355/**
1356 * Entry point to this code when a new device is created. Allocate the basic
1357 * structures and the ring buffer for communication with the backend, and
1358 * inform the backend of the appropriate details for those. Switch to
1359 * Initialised state.
1360 */
1361static int blkfront_probe(struct xenbus_device *dev,
1362 const struct xenbus_device_id *id)
1363{
1364 int err, vdevice, i;
1365 struct blkfront_info *info;
1366
1367 /* FIXME: Use dynamic device id if this is not set. */
1368 err = xenbus_scanf(XBT_NIL, dev->nodename,
1369 "virtual-device", "%i", &vdevice);
1370 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001371 /* go looking in the extended area instead */
1372 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1373 "%i", &vdevice);
1374 if (err != 1) {
1375 xenbus_dev_fatal(dev, err, "reading virtual-device");
1376 return err;
1377 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001378 }
1379
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001380 if (xen_hvm_domain()) {
1381 char *type;
1382 int len;
1383 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001384 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001385 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001386
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001387 if (!VDEV_IS_EXTENDED(vdevice))
1388 major = BLKIF_MAJOR(vdevice);
1389 else
1390 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001391
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001392 if (major != XENVBD_MAJOR) {
1393 printk(KERN_INFO
1394 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001395 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001396 return -ENODEV;
1397 }
1398 }
1399 /* do not create a PV cdrom device if we are an HVM guest */
1400 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1401 if (IS_ERR(type))
1402 return -ENODEV;
1403 if (strncmp(type, "cdrom", 5) == 0) {
1404 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001405 return -ENODEV;
1406 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001407 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001408 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001409 info = kzalloc(sizeof(*info), GFP_KERNEL);
1410 if (!info) {
1411 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1412 return -ENOMEM;
1413 }
1414
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001415 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001416 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001417 info->xbdev = dev;
1418 info->vdevice = vdevice;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001419 INIT_LIST_HEAD(&info->grants);
1420 INIT_LIST_HEAD(&info->indirect_pages);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001421 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001422 info->connected = BLKIF_STATE_DISCONNECTED;
1423 INIT_WORK(&info->work, blkif_restart_queue);
1424
1425 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001426 info->shadow[i].req.u.rw.id = i+1;
1427 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001428
1429 /* Front end dir is a number, which is used as the id. */
1430 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001431 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001432
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001433 return 0;
1434}
1435
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001436static void split_bio_end(struct bio *bio, int error)
1437{
1438 struct split_bio *split_bio = bio->bi_private;
1439
1440 if (error)
1441 split_bio->err = error;
1442
1443 if (atomic_dec_and_test(&split_bio->pending)) {
1444 split_bio->bio->bi_phys_segments = 0;
1445 bio_endio(split_bio->bio, split_bio->err);
1446 kfree(split_bio);
1447 }
1448 bio_put(bio);
1449}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001450
1451static int blkif_recover(struct blkfront_info *info)
1452{
1453 int i;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001454 struct request *req, *n;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001455 struct blk_shadow *copy;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001456 int rc;
1457 struct bio *bio, *cloned_bio;
1458 struct bio_list bio_list, merge_bio;
1459 unsigned int segs, offset;
1460 int pending, size;
1461 struct split_bio *split_bio;
1462 struct list_head requests;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001463
1464 /* Stage 1: Make a safe copy of the shadow state. */
Mihnea Dobrescu-Balaur29d0b212013-03-11 13:23:36 +02001465 copy = kmemdup(info->shadow, sizeof(info->shadow),
Ian Campbella144ff02008-06-17 10:47:08 +02001466 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001467 if (!copy)
1468 return -ENOMEM;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001469
1470 /* Stage 2: Set up free list. */
1471 memset(&info->shadow, 0, sizeof(info->shadow));
1472 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001473 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001474 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001475 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001476
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001477 rc = blkfront_setup_indirect(info);
1478 if (rc) {
1479 kfree(copy);
1480 return rc;
1481 }
1482
1483 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1484 blk_queue_max_segments(info->rq, segs);
1485 bio_list_init(&bio_list);
1486 INIT_LIST_HEAD(&requests);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001487 for (i = 0; i < BLK_RING_SIZE; i++) {
1488 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001489 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001490 continue;
1491
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001492 /*
1493 * Get the bios in the request so we can re-queue them.
1494 */
1495 if (copy[i].request->cmd_flags &
1496 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1497 /*
1498 * Flush operations don't contain bios, so
1499 * we need to requeue the whole request
1500 */
1501 list_add(&copy[i].request->queuelist, &requests);
1502 continue;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001503 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001504 merge_bio.head = copy[i].request->bio;
1505 merge_bio.tail = copy[i].request->biotail;
1506 bio_list_merge(&bio_list, &merge_bio);
1507 copy[i].request->bio = NULL;
Roger Pau Monne3bb8c982015-02-02 11:28:21 +00001508 blk_end_request_all(copy[i].request, 0);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001509 }
1510
1511 kfree(copy);
1512
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001513 /*
1514 * Empty the queue, this is important because we might have
1515 * requests in the queue with more segments than what we
1516 * can handle now.
1517 */
1518 spin_lock_irq(&info->io_lock);
1519 while ((req = blk_fetch_request(info->rq)) != NULL) {
1520 if (req->cmd_flags &
1521 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1522 list_add(&req->queuelist, &requests);
1523 continue;
1524 }
1525 merge_bio.head = req->bio;
1526 merge_bio.tail = req->biotail;
1527 bio_list_merge(&bio_list, &merge_bio);
1528 req->bio = NULL;
1529 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1530 pr_alert("diskcache flush request found!\n");
Roger Pau Monne3bb8c982015-02-02 11:28:21 +00001531 __blk_end_request_all(req, 0);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001532 }
1533 spin_unlock_irq(&info->io_lock);
1534
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001535 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1536
Steven Noonan34678112012-02-17 12:04:44 -08001537 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001538
1539 /* Now safe for us to use the shared ring */
1540 info->connected = BLKIF_STATE_CONNECTED;
1541
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001542 /* Kick any other new requests queued since we resumed */
1543 kick_pending_request_queues(info);
1544
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001545 list_for_each_entry_safe(req, n, &requests, queuelist) {
1546 /* Requeue pending requests (flush or discard) */
1547 list_del_init(&req->queuelist);
1548 BUG_ON(req->nr_phys_segments > segs);
1549 blk_requeue_request(info->rq, req);
1550 }
Steven Noonan34678112012-02-17 12:04:44 -08001551 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001552
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001553 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1554 /* Traverse the list of pending bios and re-queue them */
1555 if (bio_segments(bio) > segs) {
1556 /*
1557 * This bio has more segments than what we can
1558 * handle, we have to split it.
1559 */
1560 pending = (bio_segments(bio) + segs - 1) / segs;
1561 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1562 BUG_ON(split_bio == NULL);
1563 atomic_set(&split_bio->pending, pending);
1564 split_bio->bio = bio;
1565 for (i = 0; i < pending; i++) {
1566 offset = (i * segs * PAGE_SIZE) >> 9;
1567 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001568 (unsigned int)bio_sectors(bio) - offset);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001569 cloned_bio = bio_clone(bio, GFP_NOIO);
1570 BUG_ON(cloned_bio == NULL);
Kent Overstreet6678d832013-08-07 11:14:32 -07001571 bio_trim(cloned_bio, offset, size);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001572 cloned_bio->bi_private = split_bio;
1573 cloned_bio->bi_end_io = split_bio_end;
1574 submit_bio(cloned_bio->bi_rw, cloned_bio);
1575 }
1576 /*
1577 * Now we have to wait for all those smaller bios to
1578 * end, so we can also end the "parent" bio.
1579 */
1580 continue;
1581 }
1582 /* We don't need to split this bio */
1583 submit_bio(bio->bi_rw, bio);
1584 }
1585
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001586 return 0;
1587}
1588
1589/**
1590 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1591 * driver restart. We tear down our blkif structure and recreate it, but
1592 * leave the device-layer structures intact so that this is transparent to the
1593 * rest of the kernel.
1594 */
1595static int blkfront_resume(struct xenbus_device *dev)
1596{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001597 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001598 int err;
1599
1600 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1601
1602 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1603
Ian Campbell203fd612009-12-04 15:33:54 +00001604 err = talk_to_blkback(dev, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001605
1606 /*
1607 * We have to wait for the backend to switch to
1608 * connected state, since we want to read which
1609 * features it supports.
1610 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001611
1612 return err;
1613}
1614
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001615static void
1616blkfront_closing(struct blkfront_info *info)
1617{
1618 struct xenbus_device *xbdev = info->xbdev;
1619 struct block_device *bdev = NULL;
1620
1621 mutex_lock(&info->mutex);
1622
1623 if (xbdev->state == XenbusStateClosing) {
1624 mutex_unlock(&info->mutex);
1625 return;
1626 }
1627
1628 if (info->gd)
1629 bdev = bdget_disk(info->gd, 0);
1630
1631 mutex_unlock(&info->mutex);
1632
1633 if (!bdev) {
1634 xenbus_frontend_closed(xbdev);
1635 return;
1636 }
1637
1638 mutex_lock(&bdev->bd_mutex);
1639
Daniel Stodden7b32d102010-04-30 22:01:23 +00001640 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001641 xenbus_dev_error(xbdev, -EBUSY,
1642 "Device in use; refusing to close");
1643 xenbus_switch_state(xbdev, XenbusStateClosing);
1644 } else {
1645 xlvbd_release_gendisk(info);
1646 xenbus_frontend_closed(xbdev);
1647 }
1648
1649 mutex_unlock(&bdev->bd_mutex);
1650 bdput(bdev);
1651}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001652
Li Dongyanged30bf32011-09-01 18:39:09 +08001653static void blkfront_setup_discard(struct blkfront_info *info)
1654{
1655 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08001656 unsigned int discard_granularity;
1657 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001658 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001659
Olaf Hering1c8cad62014-05-21 16:32:40 +02001660 info->feature_discard = 1;
1661 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1662 "discard-granularity", "%u", &discard_granularity,
1663 "discard-alignment", "%u", &discard_alignment,
1664 NULL);
1665 if (!err) {
1666 info->discard_granularity = discard_granularity;
1667 info->discard_alignment = discard_alignment;
1668 }
1669 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1670 "discard-secure", "%d", &discard_secure,
1671 NULL);
1672 if (!err)
1673 info->feature_secdiscard = !!discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001674}
1675
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001676static int blkfront_setup_indirect(struct blkfront_info *info)
1677{
1678 unsigned int indirect_segments, segs;
1679 int err, i;
1680
1681 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1682 "feature-max-indirect-segments", "%u", &indirect_segments,
1683 NULL);
1684 if (err) {
1685 info->max_indirect_segments = 0;
1686 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1687 } else {
1688 info->max_indirect_segments = min(indirect_segments,
1689 xen_blkif_max_segments);
1690 segs = info->max_indirect_segments;
1691 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001692
1693 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
1694 if (err)
1695 goto out_of_memory;
1696
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001697 if (!info->feature_persistent && info->max_indirect_segments) {
1698 /*
1699 * We are using indirect descriptors but not persistent
1700 * grants, we need to allocate a set of pages that can be
1701 * used for mapping indirect grefs
1702 */
1703 int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE;
1704
1705 BUG_ON(!list_empty(&info->indirect_pages));
1706 for (i = 0; i < num; i++) {
1707 struct page *indirect_page = alloc_page(GFP_NOIO);
1708 if (!indirect_page)
1709 goto out_of_memory;
1710 list_add(&indirect_page->lru, &info->indirect_pages);
1711 }
1712 }
1713
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001714 for (i = 0; i < BLK_RING_SIZE; i++) {
1715 info->shadow[i].grants_used = kzalloc(
1716 sizeof(info->shadow[i].grants_used[0]) * segs,
1717 GFP_NOIO);
Roger Pau Monneb7649152013-05-02 10:58:50 +02001718 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001719 if (info->max_indirect_segments)
1720 info->shadow[i].indirect_grants = kzalloc(
1721 sizeof(info->shadow[i].indirect_grants[0]) *
1722 INDIRECT_GREFS(segs),
1723 GFP_NOIO);
1724 if ((info->shadow[i].grants_used == NULL) ||
Roger Pau Monneb7649152013-05-02 10:58:50 +02001725 (info->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001726 (info->max_indirect_segments &&
1727 (info->shadow[i].indirect_grants == NULL)))
1728 goto out_of_memory;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001729 sg_init_table(info->shadow[i].sg, segs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001730 }
1731
1732
1733 return 0;
1734
1735out_of_memory:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001736 for (i = 0; i < BLK_RING_SIZE; i++) {
1737 kfree(info->shadow[i].grants_used);
1738 info->shadow[i].grants_used = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001739 kfree(info->shadow[i].sg);
1740 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001741 kfree(info->shadow[i].indirect_grants);
1742 info->shadow[i].indirect_grants = NULL;
1743 }
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001744 if (!list_empty(&info->indirect_pages)) {
1745 struct page *indirect_page, *n;
1746 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1747 list_del(&indirect_page->lru);
1748 __free_page(indirect_page);
1749 }
1750 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001751 return -ENOMEM;
1752}
1753
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001754/*
1755 * Invoked when the backend is finally 'ready' (and has told produced
1756 * the details about the physical device - #sectors, size, etc).
1757 */
1758static void blkfront_connect(struct blkfront_info *info)
1759{
1760 unsigned long long sectors;
1761 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001762 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001763 unsigned int binfo;
1764 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001765 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001766
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001767 switch (info->connected) {
1768 case BLKIF_STATE_CONNECTED:
1769 /*
1770 * Potentially, the back-end may be signalling
1771 * a capacity change; update the capacity.
1772 */
1773 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1774 "sectors", "%Lu", &sectors);
1775 if (XENBUS_EXIST_ERR(err))
1776 return;
1777 printk(KERN_INFO "Setting capacity to %Lu\n",
1778 sectors);
1779 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001780 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001781
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001782 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001783 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001784 /*
1785 * If we are recovering from suspension, we need to wait
1786 * for the backend to announce it's features before
1787 * reconnecting, at least we need to know if the backend
1788 * supports indirect descriptors, and how many.
1789 */
1790 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001791 return;
1792
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001793 default:
1794 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001795 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001796
1797 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1798 __func__, info->xbdev->otherend);
1799
1800 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1801 "sectors", "%llu", &sectors,
1802 "info", "%u", &binfo,
1803 "sector-size", "%lu", &sector_size,
1804 NULL);
1805 if (err) {
1806 xenbus_dev_fatal(info->xbdev, err,
1807 "reading backend fields at %s",
1808 info->xbdev->otherend);
1809 return;
1810 }
1811
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001812 /*
1813 * physcial-sector-size is a newer field, so old backends may not
1814 * provide this. Assume physical sector size to be the same as
1815 * sector_size in that case.
1816 */
1817 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1818 "physical-sector-size", "%u", &physical_sector_size);
1819 if (err != 1)
1820 physical_sector_size = sector_size;
1821
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001822 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001823
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001824 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001825 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001826 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001827
1828 /*
1829 * If there's no "feature-barrier" defined, then it means
1830 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001831 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001832 *
Tejun Heo6958f142010-09-03 11:56:16 +02001833 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001834 */
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001835 if (!err && barrier)
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001836 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001837 /*
1838 * And if there is "feature-flush-cache" use that above
1839 * barriers.
1840 */
1841 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1842 "feature-flush-cache", "%d", &flush,
1843 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001844
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001845 if (!err && flush)
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001846 info->feature_flush = REQ_FLUSH;
Li Dongyanged30bf32011-09-01 18:39:09 +08001847
1848 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1849 "feature-discard", "%d", &discard,
1850 NULL);
1851
1852 if (!err && discard)
1853 blkfront_setup_discard(info);
1854
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001855 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1856 "feature-persistent", "%u", &persistent,
1857 NULL);
1858 if (err)
1859 info->feature_persistent = 0;
1860 else
1861 info->feature_persistent = persistent;
1862
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001863 err = blkfront_setup_indirect(info);
1864 if (err) {
1865 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1866 info->xbdev->otherend);
1867 return;
1868 }
1869
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001870 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
1871 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001872 if (err) {
1873 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1874 info->xbdev->otherend);
1875 return;
1876 }
1877
1878 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1879
1880 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001881 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001882 info->connected = BLKIF_STATE_CONNECTED;
1883 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001884 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001885
1886 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001887
1888 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001889}
1890
1891/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001892 * Callback received when the backend's state changes.
1893 */
Ian Campbell203fd612009-12-04 15:33:54 +00001894static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001895 enum xenbus_state backend_state)
1896{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001897 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001898
Ian Campbell203fd612009-12-04 15:33:54 +00001899 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001900
1901 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001902 case XenbusStateInitWait:
Bob Liu8ab01442015-06-03 13:40:02 +08001903 if (talk_to_blkback(dev, info)) {
1904 kfree(info);
1905 dev_set_drvdata(&dev->dev, NULL);
1906 break;
1907 }
1908 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001909 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001910 case XenbusStateReconfiguring:
1911 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001912 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001913 break;
1914
1915 case XenbusStateConnected:
1916 blkfront_connect(info);
1917 break;
1918
David Vrabel36613712014-02-04 18:53:56 +00001919 case XenbusStateClosed:
1920 if (dev->state == XenbusStateClosed)
1921 break;
1922 /* Missed the backend's Closing state -- fallthrough */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001923 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001924 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001925 break;
1926 }
1927}
1928
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001929static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001930{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001931 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1932 struct block_device *bdev = NULL;
1933 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001934
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001935 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001936
1937 blkif_free(info, 0);
1938
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001939 mutex_lock(&info->mutex);
1940
1941 disk = info->gd;
1942 if (disk)
1943 bdev = bdget_disk(disk, 0);
1944
1945 info->xbdev = NULL;
1946 mutex_unlock(&info->mutex);
1947
1948 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001949 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001950 return 0;
1951 }
1952
1953 /*
1954 * The xbdev was removed before we reached the Closed
1955 * state. See if it's safe to remove the disk. If the bdev
1956 * isn't closed yet, we let release take care of it.
1957 */
1958
1959 mutex_lock(&bdev->bd_mutex);
1960 info = disk->private_data;
1961
Daniel Stoddend54142c2010-08-07 18:51:21 +02001962 dev_warn(disk_to_dev(disk),
1963 "%s was hot-unplugged, %d stale handles\n",
1964 xbdev->nodename, bdev->bd_openers);
1965
Daniel Stodden7b32d102010-04-30 22:01:23 +00001966 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001967 xlvbd_release_gendisk(info);
1968 disk->private_data = NULL;
1969 kfree(info);
1970 }
1971
1972 mutex_unlock(&bdev->bd_mutex);
1973 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001974
1975 return 0;
1976}
1977
Christian Limpach1d78d702008-04-02 10:54:04 -07001978static int blkfront_is_ready(struct xenbus_device *dev)
1979{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001980 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001981
Jan Beulich5d7ed202010-08-07 18:31:12 +02001982 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001983}
1984
Al Viroa63c8482008-03-02 10:23:47 -05001985static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001986{
Daniel Stodden13961742010-08-07 18:36:53 +02001987 struct gendisk *disk = bdev->bd_disk;
1988 struct blkfront_info *info;
1989 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001990
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001991 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001992
Daniel Stodden13961742010-08-07 18:36:53 +02001993 info = disk->private_data;
1994 if (!info) {
1995 /* xbdev gone */
1996 err = -ERESTARTSYS;
1997 goto out;
1998 }
1999
2000 mutex_lock(&info->mutex);
2001
2002 if (!info->gd)
2003 /* xbdev is closed */
2004 err = -ERESTARTSYS;
2005
2006 mutex_unlock(&info->mutex);
2007
Daniel Stodden13961742010-08-07 18:36:53 +02002008out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002009 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002010 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002011}
2012
Al Virodb2a1442013-05-05 21:52:57 -04002013static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002014{
Al Viroa63c8482008-03-02 10:23:47 -05002015 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002016 struct block_device *bdev;
2017 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002018
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002019 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002020
2021 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002022
Felipe Pena2f089cb2013-11-09 13:36:09 -02002023 if (!bdev) {
2024 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2025 goto out_mutex;
2026 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002027 if (bdev->bd_openers)
2028 goto out;
2029
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002030 /*
2031 * Check if we have been instructed to close. We will have
2032 * deferred this request, because the bdev was still open.
2033 */
2034
2035 mutex_lock(&info->mutex);
2036 xbdev = info->xbdev;
2037
2038 if (xbdev && xbdev->state == XenbusStateClosing) {
2039 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002040 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002041 xlvbd_release_gendisk(info);
2042 xenbus_frontend_closed(info->xbdev);
2043 }
2044
2045 mutex_unlock(&info->mutex);
2046
2047 if (!xbdev) {
2048 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002049 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002050 xlvbd_release_gendisk(info);
2051 disk->private_data = NULL;
2052 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002053 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002054
Jens Axboea4cc14e2010-08-08 21:50:05 -04002055out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002056 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002057out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002058 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002059}
2060
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002061static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002062{
2063 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002064 .open = blkif_open,
2065 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002066 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002067 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002068};
2069
2070
Márton Némethec9c42e2010-01-10 13:39:52 +01002071static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002072 { "vbd" },
2073 { "" }
2074};
2075
David Vrabel95afae42014-09-08 17:30:41 +01002076static struct xenbus_driver blkfront_driver = {
2077 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002078 .probe = blkfront_probe,
2079 .remove = blkfront_remove,
2080 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002081 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002082 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002083};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002084
2085static int __init xlblk_init(void)
2086{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002087 int ret;
2088
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002089 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002090 return -ENODEV;
2091
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002092 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002093 return -ENODEV;
2094
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002095 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2096 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2097 XENVBD_MAJOR, DEV_NAME);
2098 return -ENODEV;
2099 }
2100
Jan Beulich73db1442011-12-22 09:08:13 +00002101 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002102 if (ret) {
2103 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2104 return ret;
2105 }
2106
2107 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002108}
2109module_init(xlblk_init);
2110
2111
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002112static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002113{
Jan Beulich86050672012-04-05 16:04:52 +01002114 xenbus_unregister_driver(&blkfront_driver);
2115 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2116 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002117}
2118module_exit(xlblk_exit);
2119
2120MODULE_DESCRIPTION("Xen virtual block device frontend");
2121MODULE_LICENSE("GPL");
2122MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002123MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002124MODULE_ALIAS("xenblk");