blob: 0b1eea64326252931ec46148c8b194261021568a [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 Bergmann6e9624b2010-08-07 18:25:34 +020044#include <linux/smp_lock.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010045#include <linux/scatterlist.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070046
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070047#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048#include <xen/xenbus.h>
49#include <xen/grant_table.h>
50#include <xen/events.h>
51#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010052#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070053
54#include <xen/interface/grant_table.h>
55#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070056#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070057
58#include <asm/xen/hypervisor.h>
59
60enum blkif_state {
61 BLKIF_STATE_DISCONNECTED,
62 BLKIF_STATE_CONNECTED,
63 BLKIF_STATE_SUSPENDED,
64};
65
66struct blk_shadow {
67 struct blkif_request req;
68 unsigned long request;
69 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
70};
71
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070072static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070073
74#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
75
76/*
77 * We have one of these per vbd, whether ide, scsi or 'other'. They
78 * hang in private_data off the gendisk structure. We may end up
79 * putting all kinds of interesting stuff here :-)
80 */
81struct blkfront_info
82{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000083 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070084 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085 struct gendisk *gd;
86 int vdevice;
87 blkif_vdev_t handle;
88 enum blkif_state connected;
89 int ring_ref;
90 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010091 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070092 unsigned int evtchn, irq;
93 struct request_queue *rq;
94 struct work_struct work;
95 struct gnttab_free_callback callback;
96 struct blk_shadow shadow[BLK_RING_SIZE];
97 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +020098 unsigned int feature_flush;
Christian Limpach1d78d702008-04-02 10:54:04 -070099 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700100};
101
102static DEFINE_SPINLOCK(blkif_io_lock);
103
Jan Beulich0e345822010-08-07 18:28:55 +0200104static unsigned int nr_minors;
105static unsigned long *minors;
106static DEFINE_SPINLOCK(minor_lock);
107
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700108#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
109 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
110#define GRANT_INVALID_REF 0
111
112#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700113#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700114
115#define BLKIF_MAJOR(dev) ((dev)>>8)
116#define BLKIF_MINOR(dev) ((dev) & 0xff)
117
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700118#define EXT_SHIFT 28
119#define EXTENDED (1<<EXT_SHIFT)
120#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
121#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700122
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700123#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700124
125static int get_id_from_freelist(struct blkfront_info *info)
126{
127 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200128 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700129 info->shadow_free = info->shadow[free].req.id;
130 info->shadow[free].req.id = 0x0fffffee; /* debug */
131 return free;
132}
133
134static void add_id_to_freelist(struct blkfront_info *info,
135 unsigned long id)
136{
137 info->shadow[id].req.id = info->shadow_free;
138 info->shadow[id].request = 0;
139 info->shadow_free = id;
140}
141
Jan Beulich0e345822010-08-07 18:28:55 +0200142static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
143{
144 unsigned int end = minor + nr;
145 int rc;
146
147 if (end > nr_minors) {
148 unsigned long *bitmap, *old;
149
150 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
151 GFP_KERNEL);
152 if (bitmap == NULL)
153 return -ENOMEM;
154
155 spin_lock(&minor_lock);
156 if (end > nr_minors) {
157 old = minors;
158 memcpy(bitmap, minors,
159 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
160 minors = bitmap;
161 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
162 } else
163 old = bitmap;
164 spin_unlock(&minor_lock);
165 kfree(old);
166 }
167
168 spin_lock(&minor_lock);
169 if (find_next_bit(minors, end, minor) >= end) {
170 for (; minor < end; ++minor)
171 __set_bit(minor, minors);
172 rc = 0;
173 } else
174 rc = -EBUSY;
175 spin_unlock(&minor_lock);
176
177 return rc;
178}
179
180static void xlbd_release_minors(unsigned int minor, unsigned int nr)
181{
182 unsigned int end = minor + nr;
183
184 BUG_ON(end > nr_minors);
185 spin_lock(&minor_lock);
186 for (; minor < end; ++minor)
187 __clear_bit(minor, minors);
188 spin_unlock(&minor_lock);
189}
190
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700191static void blkif_restart_queue_callback(void *arg)
192{
193 struct blkfront_info *info = (struct blkfront_info *)arg;
194 schedule_work(&info->work);
195}
196
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700197static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800198{
199 /* We don't have real geometry info, but let's at least return
200 values consistent with the size of the device */
201 sector_t nsect = get_capacity(bd->bd_disk);
202 sector_t cylinders = nsect;
203
204 hg->heads = 0xff;
205 hg->sectors = 0x3f;
206 sector_div(cylinders, hg->heads * hg->sectors);
207 hg->cylinders = cylinders;
208 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
209 hg->cylinders = 0xffff;
210 return 0;
211}
212
Al Viroa63c8482008-03-02 10:23:47 -0500213static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200214 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200215{
Al Viroa63c8482008-03-02 10:23:47 -0500216 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200217 int i;
218
219 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
220 command, (long)argument);
221
222 switch (command) {
223 case CDROMMULTISESSION:
224 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
225 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
226 if (put_user(0, (char __user *)(argument + i)))
227 return -EFAULT;
228 return 0;
229
230 case CDROM_GET_CAPABILITY: {
231 struct gendisk *gd = info->gd;
232 if (gd->flags & GENHD_FL_CD)
233 return 0;
234 return -EINVAL;
235 }
236
237 default:
238 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
239 command);*/
240 return -EINVAL; /* same return as native Linux */
241 }
242
243 return 0;
244}
245
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700246/*
247 * blkif_queue_request
248 *
249 * request block io
250 *
251 * id: for guest use only.
252 * operation: BLKIF_OP_{READ,WRITE,PROBE}
253 * buffer: buffer to read/write into. this should be a
254 * virtual address in the guest os.
255 */
256static int blkif_queue_request(struct request *req)
257{
258 struct blkfront_info *info = req->rq_disk->private_data;
259 unsigned long buffer_mfn;
260 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700261 unsigned long id;
262 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100263 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700264 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100265 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700266
267 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
268 return 1;
269
270 if (gnttab_alloc_grant_references(
271 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
272 gnttab_request_free_callback(
273 &info->callback,
274 blkif_restart_queue_callback,
275 info,
276 BLKIF_MAX_SEGMENTS_PER_REQUEST);
277 return 1;
278 }
279
280 /* Fill out a communications ring structure. */
281 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
282 id = get_id_from_freelist(info);
283 info->shadow[id].request = (unsigned long)req;
284
285 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900286 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700287 ring_req->handle = info->handle;
288
289 ring_req->operation = rq_data_dir(req) ?
290 BLKIF_OP_WRITE : BLKIF_OP_READ;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200291 if (req->cmd_flags & REQ_HARDBARRIER)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700292 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
293
Jens Axboe9e973e62009-02-24 08:10:09 +0100294 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
295 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
296
297 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
298 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
299 fsect = sg->offset >> 9;
300 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200301 /* install a grant reference. */
302 ref = gnttab_claim_grant_reference(&gref_head);
303 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700304
Jens Axboe6c92e692007-08-16 13:43:12 +0200305 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700306 ref,
307 info->xbdev->otherend_id,
308 buffer_mfn,
309 rq_data_dir(req) );
310
Jens Axboe9e973e62009-02-24 08:10:09 +0100311 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
312 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700313 (struct blkif_request_segment) {
314 .gref = ref,
315 .first_sect = fsect,
316 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700317 }
318
319 info->ring.req_prod_pvt++;
320
321 /* Keep a private copy so we can reissue requests when recovering. */
322 info->shadow[id].req = *ring_req;
323
324 gnttab_free_grant_references(gref_head);
325
326 return 0;
327}
328
329
330static inline void flush_requests(struct blkfront_info *info)
331{
332 int notify;
333
334 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
335
336 if (notify)
337 notify_remote_via_irq(info->irq);
338}
339
340/*
341 * do_blkif_request
342 * read a block; request is in a request queue
343 */
Jens Axboe165125e2007-07-24 09:28:11 +0200344static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700345{
346 struct blkfront_info *info = NULL;
347 struct request *req;
348 int queued;
349
350 pr_debug("Entered do_blkif_request\n");
351
352 queued = 0;
353
Tejun Heo9934c8c2009-05-08 11:54:16 +0900354 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700355 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700356
357 if (RING_FULL(&info->ring))
358 goto wait;
359
Tejun Heo9934c8c2009-05-08 11:54:16 +0900360 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900361
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200362 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900363 __blk_end_request_all(req, -EIO);
364 continue;
365 }
366
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700367 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900368 "(%u/%u) buffer:%p [%s]\n",
369 req, req->cmd, (unsigned long)blk_rq_pos(req),
370 blk_rq_cur_sectors(req), blk_rq_sectors(req),
371 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700373 if (blkif_queue_request(req)) {
374 blk_requeue_request(rq, req);
375wait:
376 /* Avoid pointless unplugs. */
377 blk_stop_queue(rq);
378 break;
379 }
380
381 queued++;
382 }
383
384 if (queued != 0)
385 flush_requests(info);
386}
387
388static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
389{
Jens Axboe165125e2007-07-24 09:28:11 +0200390 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700391
392 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
393 if (rq == NULL)
394 return -1;
395
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900396 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700397
398 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400399 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500400 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700401
402 /* Each segment in a request is up to an aligned page in size. */
403 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
404 blk_queue_max_segment_size(rq, PAGE_SIZE);
405
406 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500407 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700408
409 /* Make sure buffer addresses are sector-aligned. */
410 blk_queue_dma_alignment(rq, 511);
411
Ian Campbell1c91fe12008-06-17 10:47:08 +0200412 /* Make sure we don't use bounce buffers. */
413 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
414
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700415 gd->queue = rq;
416
417 return 0;
418}
419
420
Tejun Heo4913efe2010-09-03 11:56:16 +0200421static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700422{
Tejun Heo4913efe2010-09-03 11:56:16 +0200423 blk_queue_flush(info->rq, info->feature_flush);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700424 printk(KERN_INFO "blkfront: %s: barriers %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200425 info->gd->disk_name,
426 info->feature_flush ? "enabled" : "disabled");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700427}
428
429
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700430static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
431 struct blkfront_info *info,
432 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700433{
434 struct gendisk *gd;
435 int nr_minors = 1;
436 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700437 unsigned int offset;
438 int minor;
439 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700440
441 BUG_ON(info->gd != NULL);
442 BUG_ON(info->rq != NULL);
443
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700444 if ((info->vdevice>>EXT_SHIFT) > 1) {
445 /* this is above the extended range; something is wrong */
446 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
447 return -ENODEV;
448 }
449
450 if (!VDEV_IS_EXTENDED(info->vdevice)) {
451 minor = BLKIF_MINOR(info->vdevice);
452 nr_parts = PARTS_PER_DISK;
453 } else {
454 minor = BLKIF_MINOR_EXT(info->vdevice);
455 nr_parts = PARTS_PER_EXT_DISK;
456 }
457
458 if ((minor % nr_parts) == 0)
459 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700460
Jan Beulich0e345822010-08-07 18:28:55 +0200461 err = xlbd_reserve_minors(minor, nr_minors);
462 if (err)
463 goto out;
464 err = -ENODEV;
465
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700466 gd = alloc_disk(nr_minors);
467 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200468 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700469
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700470 offset = minor / nr_parts;
471
472 if (nr_minors > 1) {
473 if (offset < 26)
474 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
475 else
476 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
477 'a' + ((offset / 26)-1), 'a' + (offset % 26));
478 } else {
479 if (offset < 26)
480 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
481 'a' + offset,
482 minor & (nr_parts - 1));
483 else
484 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
485 'a' + ((offset / 26) - 1),
486 'a' + (offset % 26),
487 minor & (nr_parts - 1));
488 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700489
490 gd->major = XENVBD_MAJOR;
491 gd->first_minor = minor;
492 gd->fops = &xlvbd_block_fops;
493 gd->private_data = info;
494 gd->driverfs_dev = &(info->xbdev->dev);
495 set_capacity(gd, capacity);
496
497 if (xlvbd_init_blk_queue(gd, sector_size)) {
498 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200499 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700500 }
501
502 info->rq = gd->queue;
503 info->gd = gd;
504
Tejun Heo4913efe2010-09-03 11:56:16 +0200505 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700506
507 if (vdisk_info & VDISK_READONLY)
508 set_disk_ro(gd, 1);
509
510 if (vdisk_info & VDISK_REMOVABLE)
511 gd->flags |= GENHD_FL_REMOVABLE;
512
513 if (vdisk_info & VDISK_CDROM)
514 gd->flags |= GENHD_FL_CD;
515
516 return 0;
517
Jan Beulich0e345822010-08-07 18:28:55 +0200518 release:
519 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700520 out:
521 return err;
522}
523
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200524static void xlvbd_release_gendisk(struct blkfront_info *info)
525{
526 unsigned int minor, nr_minors;
527 unsigned long flags;
528
529 if (info->rq == NULL)
530 return;
531
532 spin_lock_irqsave(&blkif_io_lock, flags);
533
534 /* No more blkif_request(). */
535 blk_stop_queue(info->rq);
536
537 /* No more gnttab callback work. */
538 gnttab_cancel_free_callback(&info->callback);
539 spin_unlock_irqrestore(&blkif_io_lock, flags);
540
541 /* Flush gnttab callback work. Must be done with no locks held. */
542 flush_scheduled_work();
543
544 del_gendisk(info->gd);
545
546 minor = info->gd->first_minor;
547 nr_minors = info->gd->minors;
548 xlbd_release_minors(minor, nr_minors);
549
550 blk_cleanup_queue(info->rq);
551 info->rq = NULL;
552
553 put_disk(info->gd);
554 info->gd = NULL;
555}
556
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700557static void kick_pending_request_queues(struct blkfront_info *info)
558{
559 if (!RING_FULL(&info->ring)) {
560 /* Re-enable calldowns. */
561 blk_start_queue(info->rq);
562 /* Kick things off immediately. */
563 do_blkif_request(info->rq);
564 }
565}
566
567static void blkif_restart_queue(struct work_struct *work)
568{
569 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
570
571 spin_lock_irq(&blkif_io_lock);
572 if (info->connected == BLKIF_STATE_CONNECTED)
573 kick_pending_request_queues(info);
574 spin_unlock_irq(&blkif_io_lock);
575}
576
577static void blkif_free(struct blkfront_info *info, int suspend)
578{
579 /* Prevent new requests being issued until we fix things up. */
580 spin_lock_irq(&blkif_io_lock);
581 info->connected = suspend ?
582 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
583 /* No more blkif_request(). */
584 if (info->rq)
585 blk_stop_queue(info->rq);
586 /* No more gnttab callback work. */
587 gnttab_cancel_free_callback(&info->callback);
588 spin_unlock_irq(&blkif_io_lock);
589
590 /* Flush gnttab callback work. Must be done with no locks held. */
591 flush_scheduled_work();
592
593 /* Free resources associated with old device channel. */
594 if (info->ring_ref != GRANT_INVALID_REF) {
595 gnttab_end_foreign_access(info->ring_ref, 0,
596 (unsigned long)info->ring.sring);
597 info->ring_ref = GRANT_INVALID_REF;
598 info->ring.sring = NULL;
599 }
600 if (info->irq)
601 unbind_from_irqhandler(info->irq, info);
602 info->evtchn = info->irq = 0;
603
604}
605
606static void blkif_completion(struct blk_shadow *s)
607{
608 int i;
609 for (i = 0; i < s->req.nr_segments; i++)
610 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
611}
612
613static irqreturn_t blkif_interrupt(int irq, void *dev_id)
614{
615 struct request *req;
616 struct blkif_response *bret;
617 RING_IDX i, rp;
618 unsigned long flags;
619 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500620 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700621
622 spin_lock_irqsave(&blkif_io_lock, flags);
623
624 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
625 spin_unlock_irqrestore(&blkif_io_lock, flags);
626 return IRQ_HANDLED;
627 }
628
629 again:
630 rp = info->ring.sring->rsp_prod;
631 rmb(); /* Ensure we see queued responses up to 'rp'. */
632
633 for (i = info->ring.rsp_cons; i != rp; i++) {
634 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700635
636 bret = RING_GET_RESPONSE(&info->ring, i);
637 id = bret->id;
638 req = (struct request *)info->shadow[id].request;
639
640 blkif_completion(&info->shadow[id]);
641
642 add_id_to_freelist(info, id);
643
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500644 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700645 switch (bret->operation) {
646 case BLKIF_OP_WRITE_BARRIER:
647 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
648 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
649 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500650 error = -EOPNOTSUPP;
Tejun Heo4913efe2010-09-03 11:56:16 +0200651 info->feature_flush = 0;
652 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700653 }
654 /* fall through */
655 case BLKIF_OP_READ:
656 case BLKIF_OP_WRITE:
657 if (unlikely(bret->status != BLKIF_RSP_OKAY))
658 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
659 "request: %x\n", bret->status);
660
Tejun Heo40cbbb72009-04-23 11:05:19 +0900661 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700662 break;
663 default:
664 BUG();
665 }
666 }
667
668 info->ring.rsp_cons = i;
669
670 if (i != info->ring.req_prod_pvt) {
671 int more_to_do;
672 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
673 if (more_to_do)
674 goto again;
675 } else
676 info->ring.sring->rsp_event = i + 1;
677
678 kick_pending_request_queues(info);
679
680 spin_unlock_irqrestore(&blkif_io_lock, flags);
681
682 return IRQ_HANDLED;
683}
684
685
686static int setup_blkring(struct xenbus_device *dev,
687 struct blkfront_info *info)
688{
689 struct blkif_sring *sring;
690 int err;
691
692 info->ring_ref = GRANT_INVALID_REF;
693
Ian Campbella144ff02008-06-17 10:47:08 +0200694 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700695 if (!sring) {
696 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
697 return -ENOMEM;
698 }
699 SHARED_RING_INIT(sring);
700 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
701
Jens Axboe9e973e62009-02-24 08:10:09 +0100702 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
703
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700704 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
705 if (err < 0) {
706 free_page((unsigned long)sring);
707 info->ring.sring = NULL;
708 goto fail;
709 }
710 info->ring_ref = err;
711
712 err = xenbus_alloc_evtchn(dev, &info->evtchn);
713 if (err)
714 goto fail;
715
716 err = bind_evtchn_to_irqhandler(info->evtchn,
717 blkif_interrupt,
718 IRQF_SAMPLE_RANDOM, "blkif", info);
719 if (err <= 0) {
720 xenbus_dev_fatal(dev, err,
721 "bind_evtchn_to_irqhandler failed");
722 goto fail;
723 }
724 info->irq = err;
725
726 return 0;
727fail:
728 blkif_free(info, 0);
729 return err;
730}
731
732
733/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000734static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700735 struct blkfront_info *info)
736{
737 const char *message = NULL;
738 struct xenbus_transaction xbt;
739 int err;
740
741 /* Create shared ring, alloc event channel. */
742 err = setup_blkring(dev, info);
743 if (err)
744 goto out;
745
746again:
747 err = xenbus_transaction_start(&xbt);
748 if (err) {
749 xenbus_dev_fatal(dev, err, "starting transaction");
750 goto destroy_blkring;
751 }
752
753 err = xenbus_printf(xbt, dev->nodename,
754 "ring-ref", "%u", info->ring_ref);
755 if (err) {
756 message = "writing ring-ref";
757 goto abort_transaction;
758 }
759 err = xenbus_printf(xbt, dev->nodename,
760 "event-channel", "%u", info->evtchn);
761 if (err) {
762 message = "writing event-channel";
763 goto abort_transaction;
764 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700765 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
766 XEN_IO_PROTO_ABI_NATIVE);
767 if (err) {
768 message = "writing protocol";
769 goto abort_transaction;
770 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700771
772 err = xenbus_transaction_end(xbt, 0);
773 if (err) {
774 if (err == -EAGAIN)
775 goto again;
776 xenbus_dev_fatal(dev, err, "completing transaction");
777 goto destroy_blkring;
778 }
779
780 xenbus_switch_state(dev, XenbusStateInitialised);
781
782 return 0;
783
784 abort_transaction:
785 xenbus_transaction_end(xbt, 1);
786 if (message)
787 xenbus_dev_fatal(dev, err, "%s", message);
788 destroy_blkring:
789 blkif_free(info, 0);
790 out:
791 return err;
792}
793
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700794/**
795 * Entry point to this code when a new device is created. Allocate the basic
796 * structures and the ring buffer for communication with the backend, and
797 * inform the backend of the appropriate details for those. Switch to
798 * Initialised state.
799 */
800static int blkfront_probe(struct xenbus_device *dev,
801 const struct xenbus_device_id *id)
802{
803 int err, vdevice, i;
804 struct blkfront_info *info;
805
806 /* FIXME: Use dynamic device id if this is not set. */
807 err = xenbus_scanf(XBT_NIL, dev->nodename,
808 "virtual-device", "%i", &vdevice);
809 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700810 /* go looking in the extended area instead */
811 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
812 "%i", &vdevice);
813 if (err != 1) {
814 xenbus_dev_fatal(dev, err, "reading virtual-device");
815 return err;
816 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700817 }
818
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100819 if (xen_hvm_domain()) {
820 char *type;
821 int len;
822 /* no unplug has been done: do not hook devices != xen vbds */
823 if (xen_platform_pci_unplug & XEN_UNPLUG_IGNORE) {
824 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100825
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100826 if (!VDEV_IS_EXTENDED(vdevice))
827 major = BLKIF_MAJOR(vdevice);
828 else
829 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100830
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100831 if (major != XENVBD_MAJOR) {
832 printk(KERN_INFO
833 "%s: HVM does not support vbd %d as xen block device\n",
834 __FUNCTION__, vdevice);
835 return -ENODEV;
836 }
837 }
838 /* do not create a PV cdrom device if we are an HVM guest */
839 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
840 if (IS_ERR(type))
841 return -ENODEV;
842 if (strncmp(type, "cdrom", 5) == 0) {
843 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100844 return -ENODEV;
845 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100846 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100847 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700848 info = kzalloc(sizeof(*info), GFP_KERNEL);
849 if (!info) {
850 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
851 return -ENOMEM;
852 }
853
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000854 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700855 info->xbdev = dev;
856 info->vdevice = vdevice;
857 info->connected = BLKIF_STATE_DISCONNECTED;
858 INIT_WORK(&info->work, blkif_restart_queue);
859
860 for (i = 0; i < BLK_RING_SIZE; i++)
861 info->shadow[i].req.id = i+1;
862 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
863
864 /* Front end dir is a number, which is used as the id. */
865 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700866 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700867
Ian Campbell203fd612009-12-04 15:33:54 +0000868 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700869 if (err) {
870 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700871 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700872 return err;
873 }
874
875 return 0;
876}
877
878
879static int blkif_recover(struct blkfront_info *info)
880{
881 int i;
882 struct blkif_request *req;
883 struct blk_shadow *copy;
884 int j;
885
886 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200887 copy = kmalloc(sizeof(info->shadow),
888 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700889 if (!copy)
890 return -ENOMEM;
891 memcpy(copy, info->shadow, sizeof(info->shadow));
892
893 /* Stage 2: Set up free list. */
894 memset(&info->shadow, 0, sizeof(info->shadow));
895 for (i = 0; i < BLK_RING_SIZE; i++)
896 info->shadow[i].req.id = i+1;
897 info->shadow_free = info->ring.req_prod_pvt;
898 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
899
900 /* Stage 3: Find pending requests and requeue them. */
901 for (i = 0; i < BLK_RING_SIZE; i++) {
902 /* Not in use? */
903 if (copy[i].request == 0)
904 continue;
905
906 /* Grab a request slot and copy shadow state into it. */
907 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
908 *req = copy[i].req;
909
910 /* We get a new request id, and must reset the shadow state. */
911 req->id = get_id_from_freelist(info);
912 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
913
914 /* Rewrite any grant references invalidated by susp/resume. */
915 for (j = 0; j < req->nr_segments; j++)
916 gnttab_grant_foreign_access_ref(
917 req->seg[j].gref,
918 info->xbdev->otherend_id,
919 pfn_to_mfn(info->shadow[req->id].frame[j]),
920 rq_data_dir(
921 (struct request *)
922 info->shadow[req->id].request));
923 info->shadow[req->id].req = *req;
924
925 info->ring.req_prod_pvt++;
926 }
927
928 kfree(copy);
929
930 xenbus_switch_state(info->xbdev, XenbusStateConnected);
931
932 spin_lock_irq(&blkif_io_lock);
933
934 /* Now safe for us to use the shared ring */
935 info->connected = BLKIF_STATE_CONNECTED;
936
937 /* Send off requeued requests */
938 flush_requests(info);
939
940 /* Kick any other new requests queued since we resumed */
941 kick_pending_request_queues(info);
942
943 spin_unlock_irq(&blkif_io_lock);
944
945 return 0;
946}
947
948/**
949 * We are reconnecting to the backend, due to a suspend/resume, or a backend
950 * driver restart. We tear down our blkif structure and recreate it, but
951 * leave the device-layer structures intact so that this is transparent to the
952 * rest of the kernel.
953 */
954static int blkfront_resume(struct xenbus_device *dev)
955{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700956 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700957 int err;
958
959 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
960
961 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
962
Ian Campbell203fd612009-12-04 15:33:54 +0000963 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700964 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
965 err = blkif_recover(info);
966
967 return err;
968}
969
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000970static void
971blkfront_closing(struct blkfront_info *info)
972{
973 struct xenbus_device *xbdev = info->xbdev;
974 struct block_device *bdev = NULL;
975
976 mutex_lock(&info->mutex);
977
978 if (xbdev->state == XenbusStateClosing) {
979 mutex_unlock(&info->mutex);
980 return;
981 }
982
983 if (info->gd)
984 bdev = bdget_disk(info->gd, 0);
985
986 mutex_unlock(&info->mutex);
987
988 if (!bdev) {
989 xenbus_frontend_closed(xbdev);
990 return;
991 }
992
993 mutex_lock(&bdev->bd_mutex);
994
Daniel Stodden7b32d102010-04-30 22:01:23 +0000995 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000996 xenbus_dev_error(xbdev, -EBUSY,
997 "Device in use; refusing to close");
998 xenbus_switch_state(xbdev, XenbusStateClosing);
999 } else {
1000 xlvbd_release_gendisk(info);
1001 xenbus_frontend_closed(xbdev);
1002 }
1003
1004 mutex_unlock(&bdev->bd_mutex);
1005 bdput(bdev);
1006}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001007
1008/*
1009 * Invoked when the backend is finally 'ready' (and has told produced
1010 * the details about the physical device - #sectors, size, etc).
1011 */
1012static void blkfront_connect(struct blkfront_info *info)
1013{
1014 unsigned long long sectors;
1015 unsigned long sector_size;
1016 unsigned int binfo;
1017 int err;
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001018 int barrier;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001019
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001020 switch (info->connected) {
1021 case BLKIF_STATE_CONNECTED:
1022 /*
1023 * Potentially, the back-end may be signalling
1024 * a capacity change; update the capacity.
1025 */
1026 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1027 "sectors", "%Lu", &sectors);
1028 if (XENBUS_EXIST_ERR(err))
1029 return;
1030 printk(KERN_INFO "Setting capacity to %Lu\n",
1031 sectors);
1032 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001033 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001034
1035 /* fall through */
1036 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001037 return;
1038
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001039 default:
1040 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001041 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001042
1043 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1044 __func__, info->xbdev->otherend);
1045
1046 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1047 "sectors", "%llu", &sectors,
1048 "info", "%u", &binfo,
1049 "sector-size", "%lu", &sector_size,
1050 NULL);
1051 if (err) {
1052 xenbus_dev_fatal(info->xbdev, err,
1053 "reading backend fields at %s",
1054 info->xbdev->otherend);
1055 return;
1056 }
1057
1058 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001059 "feature-barrier", "%lu", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001060 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001061
1062 /*
1063 * If there's no "feature-barrier" defined, then it means
1064 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001065 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001066 *
Tejun Heo6958f142010-09-03 11:56:16 +02001067 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001068 */
Tejun Heo4913efe2010-09-03 11:56:16 +02001069 info->feature_flush = 0;
1070 if (!err && barrier)
1071 info->feature_flush = REQ_FLUSH;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001072
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001073 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001074 if (err) {
1075 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1076 info->xbdev->otherend);
1077 return;
1078 }
1079
1080 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1081
1082 /* Kick pending requests. */
1083 spin_lock_irq(&blkif_io_lock);
1084 info->connected = BLKIF_STATE_CONNECTED;
1085 kick_pending_request_queues(info);
1086 spin_unlock_irq(&blkif_io_lock);
1087
1088 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001089
1090 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001091}
1092
1093/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001094 * Callback received when the backend's state changes.
1095 */
Ian Campbell203fd612009-12-04 15:33:54 +00001096static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001097 enum xenbus_state backend_state)
1098{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001099 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001100
Ian Campbell203fd612009-12-04 15:33:54 +00001101 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001102
1103 switch (backend_state) {
1104 case XenbusStateInitialising:
1105 case XenbusStateInitWait:
1106 case XenbusStateInitialised:
1107 case XenbusStateUnknown:
1108 case XenbusStateClosed:
1109 break;
1110
1111 case XenbusStateConnected:
1112 blkfront_connect(info);
1113 break;
1114
1115 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001116 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001117 break;
1118 }
1119}
1120
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001121static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001122{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001123 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1124 struct block_device *bdev = NULL;
1125 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001126
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001127 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001128
1129 blkif_free(info, 0);
1130
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001131 mutex_lock(&info->mutex);
1132
1133 disk = info->gd;
1134 if (disk)
1135 bdev = bdget_disk(disk, 0);
1136
1137 info->xbdev = NULL;
1138 mutex_unlock(&info->mutex);
1139
1140 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001141 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001142 return 0;
1143 }
1144
1145 /*
1146 * The xbdev was removed before we reached the Closed
1147 * state. See if it's safe to remove the disk. If the bdev
1148 * isn't closed yet, we let release take care of it.
1149 */
1150
1151 mutex_lock(&bdev->bd_mutex);
1152 info = disk->private_data;
1153
Daniel Stoddend54142c2010-08-07 18:51:21 +02001154 dev_warn(disk_to_dev(disk),
1155 "%s was hot-unplugged, %d stale handles\n",
1156 xbdev->nodename, bdev->bd_openers);
1157
Daniel Stodden7b32d102010-04-30 22:01:23 +00001158 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001159 xlvbd_release_gendisk(info);
1160 disk->private_data = NULL;
1161 kfree(info);
1162 }
1163
1164 mutex_unlock(&bdev->bd_mutex);
1165 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001166
1167 return 0;
1168}
1169
Christian Limpach1d78d702008-04-02 10:54:04 -07001170static int blkfront_is_ready(struct xenbus_device *dev)
1171{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001172 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001173
Jan Beulich5d7ed202010-08-07 18:31:12 +02001174 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001175}
1176
Al Viroa63c8482008-03-02 10:23:47 -05001177static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001178{
Daniel Stodden13961742010-08-07 18:36:53 +02001179 struct gendisk *disk = bdev->bd_disk;
1180 struct blkfront_info *info;
1181 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001182
1183 lock_kernel();
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001184
Daniel Stodden13961742010-08-07 18:36:53 +02001185 info = disk->private_data;
1186 if (!info) {
1187 /* xbdev gone */
1188 err = -ERESTARTSYS;
1189 goto out;
1190 }
1191
1192 mutex_lock(&info->mutex);
1193
1194 if (!info->gd)
1195 /* xbdev is closed */
1196 err = -ERESTARTSYS;
1197
1198 mutex_unlock(&info->mutex);
1199
Daniel Stodden13961742010-08-07 18:36:53 +02001200out:
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001201 unlock_kernel();
Daniel Stodden13961742010-08-07 18:36:53 +02001202 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001203}
1204
Al Viroa63c8482008-03-02 10:23:47 -05001205static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001206{
Al Viroa63c8482008-03-02 10:23:47 -05001207 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001208 struct block_device *bdev;
1209 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001210
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001211 lock_kernel();
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001212
1213 bdev = bdget_disk(disk, 0);
1214 bdput(bdev);
1215
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001216 if (bdev->bd_openers)
1217 goto out;
1218
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001219 /*
1220 * Check if we have been instructed to close. We will have
1221 * deferred this request, because the bdev was still open.
1222 */
1223
1224 mutex_lock(&info->mutex);
1225 xbdev = info->xbdev;
1226
1227 if (xbdev && xbdev->state == XenbusStateClosing) {
1228 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001229 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001230 xlvbd_release_gendisk(info);
1231 xenbus_frontend_closed(info->xbdev);
1232 }
1233
1234 mutex_unlock(&info->mutex);
1235
1236 if (!xbdev) {
1237 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001238 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001239 xlvbd_release_gendisk(info);
1240 disk->private_data = NULL;
1241 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001242 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001243
Jens Axboea4cc14e2010-08-08 21:50:05 -04001244out:
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001245 unlock_kernel();
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001246 return 0;
1247}
1248
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001249static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001250{
1251 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001252 .open = blkif_open,
1253 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001254 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001255 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001256};
1257
1258
Márton Némethec9c42e2010-01-10 13:39:52 +01001259static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001260 { "vbd" },
1261 { "" }
1262};
1263
1264static struct xenbus_driver blkfront = {
1265 .name = "vbd",
1266 .owner = THIS_MODULE,
1267 .ids = blkfront_ids,
1268 .probe = blkfront_probe,
1269 .remove = blkfront_remove,
1270 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001271 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001272 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001273};
1274
1275static int __init xlblk_init(void)
1276{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001277 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001278 return -ENODEV;
1279
1280 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1281 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1282 XENVBD_MAJOR, DEV_NAME);
1283 return -ENODEV;
1284 }
1285
1286 return xenbus_register_frontend(&blkfront);
1287}
1288module_init(xlblk_init);
1289
1290
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001291static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001292{
1293 return xenbus_unregister_driver(&blkfront);
1294}
1295module_exit(xlblk_exit);
1296
1297MODULE_DESCRIPTION("Xen virtual block device frontend");
1298MODULE_LICENSE("GPL");
1299MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001300MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001301MODULE_ALIAS("xenblk");