blob: 255035cfc88ab8ff3a279578487facd6d17a113f [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>
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
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070073static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074
75#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
76
77/*
78 * We have one of these per vbd, whether ide, scsi or 'other'. They
79 * hang in private_data off the gendisk structure. We may end up
80 * putting all kinds of interesting stuff here :-)
81 */
82struct blkfront_info
83{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000084 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070086 struct gendisk *gd;
87 int vdevice;
88 blkif_vdev_t handle;
89 enum blkif_state connected;
90 int ring_ref;
91 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010092 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070093 unsigned int evtchn, irq;
94 struct request_queue *rq;
95 struct work_struct work;
96 struct gnttab_free_callback callback;
97 struct blk_shadow shadow[BLK_RING_SIZE];
98 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +020099 unsigned int feature_flush;
Christian Limpach1d78d702008-04-02 10:54:04 -0700100 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700101};
102
103static DEFINE_SPINLOCK(blkif_io_lock);
104
Jan Beulich0e345822010-08-07 18:28:55 +0200105static unsigned int nr_minors;
106static unsigned long *minors;
107static DEFINE_SPINLOCK(minor_lock);
108
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700109#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
110 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
111#define GRANT_INVALID_REF 0
112
113#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700114#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700115
116#define BLKIF_MAJOR(dev) ((dev)>>8)
117#define BLKIF_MINOR(dev) ((dev) & 0xff)
118
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700119#define EXT_SHIFT 28
120#define EXTENDED (1<<EXT_SHIFT)
121#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
122#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700123
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700124#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700125
126static int get_id_from_freelist(struct blkfront_info *info)
127{
128 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200129 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700130 info->shadow_free = info->shadow[free].req.id;
131 info->shadow[free].req.id = 0x0fffffee; /* debug */
132 return free;
133}
134
135static void add_id_to_freelist(struct blkfront_info *info,
136 unsigned long id)
137{
138 info->shadow[id].req.id = info->shadow_free;
139 info->shadow[id].request = 0;
140 info->shadow_free = id;
141}
142
Jan Beulich0e345822010-08-07 18:28:55 +0200143static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
144{
145 unsigned int end = minor + nr;
146 int rc;
147
148 if (end > nr_minors) {
149 unsigned long *bitmap, *old;
150
151 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
152 GFP_KERNEL);
153 if (bitmap == NULL)
154 return -ENOMEM;
155
156 spin_lock(&minor_lock);
157 if (end > nr_minors) {
158 old = minors;
159 memcpy(bitmap, minors,
160 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
161 minors = bitmap;
162 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
163 } else
164 old = bitmap;
165 spin_unlock(&minor_lock);
166 kfree(old);
167 }
168
169 spin_lock(&minor_lock);
170 if (find_next_bit(minors, end, minor) >= end) {
171 for (; minor < end; ++minor)
172 __set_bit(minor, minors);
173 rc = 0;
174 } else
175 rc = -EBUSY;
176 spin_unlock(&minor_lock);
177
178 return rc;
179}
180
181static void xlbd_release_minors(unsigned int minor, unsigned int nr)
182{
183 unsigned int end = minor + nr;
184
185 BUG_ON(end > nr_minors);
186 spin_lock(&minor_lock);
187 for (; minor < end; ++minor)
188 __clear_bit(minor, minors);
189 spin_unlock(&minor_lock);
190}
191
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700192static void blkif_restart_queue_callback(void *arg)
193{
194 struct blkfront_info *info = (struct blkfront_info *)arg;
195 schedule_work(&info->work);
196}
197
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700198static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800199{
200 /* We don't have real geometry info, but let's at least return
201 values consistent with the size of the device */
202 sector_t nsect = get_capacity(bd->bd_disk);
203 sector_t cylinders = nsect;
204
205 hg->heads = 0xff;
206 hg->sectors = 0x3f;
207 sector_div(cylinders, hg->heads * hg->sectors);
208 hg->cylinders = cylinders;
209 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
210 hg->cylinders = 0xffff;
211 return 0;
212}
213
Al Viroa63c8482008-03-02 10:23:47 -0500214static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200215 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200216{
Al Viroa63c8482008-03-02 10:23:47 -0500217 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200218 int i;
219
220 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
221 command, (long)argument);
222
223 switch (command) {
224 case CDROMMULTISESSION:
225 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
226 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
227 if (put_user(0, (char __user *)(argument + i)))
228 return -EFAULT;
229 return 0;
230
231 case CDROM_GET_CAPABILITY: {
232 struct gendisk *gd = info->gd;
233 if (gd->flags & GENHD_FL_CD)
234 return 0;
235 return -EINVAL;
236 }
237
238 default:
239 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
240 command);*/
241 return -EINVAL; /* same return as native Linux */
242 }
243
244 return 0;
245}
246
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700247/*
248 * blkif_queue_request
249 *
250 * request block io
251 *
252 * id: for guest use only.
253 * operation: BLKIF_OP_{READ,WRITE,PROBE}
254 * buffer: buffer to read/write into. this should be a
255 * virtual address in the guest os.
256 */
257static int blkif_queue_request(struct request *req)
258{
259 struct blkfront_info *info = req->rq_disk->private_data;
260 unsigned long buffer_mfn;
261 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700262 unsigned long id;
263 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100264 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700265 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100266 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700267
268 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
269 return 1;
270
271 if (gnttab_alloc_grant_references(
272 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
273 gnttab_request_free_callback(
274 &info->callback,
275 blkif_restart_queue_callback,
276 info,
277 BLKIF_MAX_SEGMENTS_PER_REQUEST);
278 return 1;
279 }
280
281 /* Fill out a communications ring structure. */
282 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
283 id = get_id_from_freelist(info);
284 info->shadow[id].request = (unsigned long)req;
285
286 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900287 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700288 ring_req->handle = info->handle;
289
290 ring_req->operation = rq_data_dir(req) ?
291 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700292
Jens Axboe9e973e62009-02-24 08:10:09 +0100293 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
294 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
295
296 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
297 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
298 fsect = sg->offset >> 9;
299 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200300 /* install a grant reference. */
301 ref = gnttab_claim_grant_reference(&gref_head);
302 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700303
Jens Axboe6c92e692007-08-16 13:43:12 +0200304 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700305 ref,
306 info->xbdev->otherend_id,
307 buffer_mfn,
308 rq_data_dir(req) );
309
Jens Axboe9e973e62009-02-24 08:10:09 +0100310 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
311 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700312 (struct blkif_request_segment) {
313 .gref = ref,
314 .first_sect = fsect,
315 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700316 }
317
318 info->ring.req_prod_pvt++;
319
320 /* Keep a private copy so we can reissue requests when recovering. */
321 info->shadow[id].req = *ring_req;
322
323 gnttab_free_grant_references(gref_head);
324
325 return 0;
326}
327
328
329static inline void flush_requests(struct blkfront_info *info)
330{
331 int notify;
332
333 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
334
335 if (notify)
336 notify_remote_via_irq(info->irq);
337}
338
339/*
340 * do_blkif_request
341 * read a block; request is in a request queue
342 */
Jens Axboe165125e2007-07-24 09:28:11 +0200343static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700344{
345 struct blkfront_info *info = NULL;
346 struct request *req;
347 int queued;
348
349 pr_debug("Entered do_blkif_request\n");
350
351 queued = 0;
352
Tejun Heo9934c8c2009-05-08 11:54:16 +0900353 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700354 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700355
356 if (RING_FULL(&info->ring))
357 goto wait;
358
Tejun Heo9934c8c2009-05-08 11:54:16 +0900359 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900360
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200361 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900362 __blk_end_request_all(req, -EIO);
363 continue;
364 }
365
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700366 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900367 "(%u/%u) buffer:%p [%s]\n",
368 req, req->cmd, (unsigned long)blk_rq_pos(req),
369 blk_rq_cur_sectors(req), blk_rq_sectors(req),
370 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700371
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372 if (blkif_queue_request(req)) {
373 blk_requeue_request(rq, req);
374wait:
375 /* Avoid pointless unplugs. */
376 blk_stop_queue(rq);
377 break;
378 }
379
380 queued++;
381 }
382
383 if (queued != 0)
384 flush_requests(info);
385}
386
387static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
388{
Jens Axboe165125e2007-07-24 09:28:11 +0200389 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700390
391 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
392 if (rq == NULL)
393 return -1;
394
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900395 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700396
397 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400398 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500399 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700400
401 /* Each segment in a request is up to an aligned page in size. */
402 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
403 blk_queue_max_segment_size(rq, PAGE_SIZE);
404
405 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500406 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700407
408 /* Make sure buffer addresses are sector-aligned. */
409 blk_queue_dma_alignment(rq, 511);
410
Ian Campbell1c91fe12008-06-17 10:47:08 +0200411 /* Make sure we don't use bounce buffers. */
412 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
413
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700414 gd->queue = rq;
415
416 return 0;
417}
418
419
Tejun Heo4913efe2010-09-03 11:56:16 +0200420static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700421{
Tejun Heo4913efe2010-09-03 11:56:16 +0200422 blk_queue_flush(info->rq, info->feature_flush);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700423 printk(KERN_INFO "blkfront: %s: barriers %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200424 info->gd->disk_name,
425 info->feature_flush ? "enabled" : "disabled");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700426}
427
428
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700429static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
430 struct blkfront_info *info,
431 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700432{
433 struct gendisk *gd;
434 int nr_minors = 1;
435 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700436 unsigned int offset;
437 int minor;
438 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700439
440 BUG_ON(info->gd != NULL);
441 BUG_ON(info->rq != NULL);
442
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700443 if ((info->vdevice>>EXT_SHIFT) > 1) {
444 /* this is above the extended range; something is wrong */
445 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
446 return -ENODEV;
447 }
448
449 if (!VDEV_IS_EXTENDED(info->vdevice)) {
450 minor = BLKIF_MINOR(info->vdevice);
451 nr_parts = PARTS_PER_DISK;
452 } else {
453 minor = BLKIF_MINOR_EXT(info->vdevice);
454 nr_parts = PARTS_PER_EXT_DISK;
455 }
456
457 if ((minor % nr_parts) == 0)
458 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700459
Jan Beulich0e345822010-08-07 18:28:55 +0200460 err = xlbd_reserve_minors(minor, nr_minors);
461 if (err)
462 goto out;
463 err = -ENODEV;
464
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700465 gd = alloc_disk(nr_minors);
466 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200467 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700468
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700469 offset = minor / nr_parts;
470
471 if (nr_minors > 1) {
472 if (offset < 26)
473 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
474 else
475 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
476 'a' + ((offset / 26)-1), 'a' + (offset % 26));
477 } else {
478 if (offset < 26)
479 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
480 'a' + offset,
481 minor & (nr_parts - 1));
482 else
483 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
484 'a' + ((offset / 26) - 1),
485 'a' + (offset % 26),
486 minor & (nr_parts - 1));
487 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700488
489 gd->major = XENVBD_MAJOR;
490 gd->first_minor = minor;
491 gd->fops = &xlvbd_block_fops;
492 gd->private_data = info;
493 gd->driverfs_dev = &(info->xbdev->dev);
494 set_capacity(gd, capacity);
495
496 if (xlvbd_init_blk_queue(gd, sector_size)) {
497 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200498 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700499 }
500
501 info->rq = gd->queue;
502 info->gd = gd;
503
Tejun Heo4913efe2010-09-03 11:56:16 +0200504 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700505
506 if (vdisk_info & VDISK_READONLY)
507 set_disk_ro(gd, 1);
508
509 if (vdisk_info & VDISK_REMOVABLE)
510 gd->flags |= GENHD_FL_REMOVABLE;
511
512 if (vdisk_info & VDISK_CDROM)
513 gd->flags |= GENHD_FL_CD;
514
515 return 0;
516
Jan Beulich0e345822010-08-07 18:28:55 +0200517 release:
518 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700519 out:
520 return err;
521}
522
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200523static void xlvbd_release_gendisk(struct blkfront_info *info)
524{
525 unsigned int minor, nr_minors;
526 unsigned long flags;
527
528 if (info->rq == NULL)
529 return;
530
531 spin_lock_irqsave(&blkif_io_lock, flags);
532
533 /* No more blkif_request(). */
534 blk_stop_queue(info->rq);
535
536 /* No more gnttab callback work. */
537 gnttab_cancel_free_callback(&info->callback);
538 spin_unlock_irqrestore(&blkif_io_lock, flags);
539
540 /* Flush gnttab callback work. Must be done with no locks held. */
541 flush_scheduled_work();
542
543 del_gendisk(info->gd);
544
545 minor = info->gd->first_minor;
546 nr_minors = info->gd->minors;
547 xlbd_release_minors(minor, nr_minors);
548
549 blk_cleanup_queue(info->rq);
550 info->rq = NULL;
551
552 put_disk(info->gd);
553 info->gd = NULL;
554}
555
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700556static void kick_pending_request_queues(struct blkfront_info *info)
557{
558 if (!RING_FULL(&info->ring)) {
559 /* Re-enable calldowns. */
560 blk_start_queue(info->rq);
561 /* Kick things off immediately. */
562 do_blkif_request(info->rq);
563 }
564}
565
566static void blkif_restart_queue(struct work_struct *work)
567{
568 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
569
570 spin_lock_irq(&blkif_io_lock);
571 if (info->connected == BLKIF_STATE_CONNECTED)
572 kick_pending_request_queues(info);
573 spin_unlock_irq(&blkif_io_lock);
574}
575
576static void blkif_free(struct blkfront_info *info, int suspend)
577{
578 /* Prevent new requests being issued until we fix things up. */
579 spin_lock_irq(&blkif_io_lock);
580 info->connected = suspend ?
581 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
582 /* No more blkif_request(). */
583 if (info->rq)
584 blk_stop_queue(info->rq);
585 /* No more gnttab callback work. */
586 gnttab_cancel_free_callback(&info->callback);
587 spin_unlock_irq(&blkif_io_lock);
588
589 /* Flush gnttab callback work. Must be done with no locks held. */
590 flush_scheduled_work();
591
592 /* Free resources associated with old device channel. */
593 if (info->ring_ref != GRANT_INVALID_REF) {
594 gnttab_end_foreign_access(info->ring_ref, 0,
595 (unsigned long)info->ring.sring);
596 info->ring_ref = GRANT_INVALID_REF;
597 info->ring.sring = NULL;
598 }
599 if (info->irq)
600 unbind_from_irqhandler(info->irq, info);
601 info->evtchn = info->irq = 0;
602
603}
604
605static void blkif_completion(struct blk_shadow *s)
606{
607 int i;
608 for (i = 0; i < s->req.nr_segments; i++)
609 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
610}
611
612static irqreturn_t blkif_interrupt(int irq, void *dev_id)
613{
614 struct request *req;
615 struct blkif_response *bret;
616 RING_IDX i, rp;
617 unsigned long flags;
618 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500619 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700620
621 spin_lock_irqsave(&blkif_io_lock, flags);
622
623 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
624 spin_unlock_irqrestore(&blkif_io_lock, flags);
625 return IRQ_HANDLED;
626 }
627
628 again:
629 rp = info->ring.sring->rsp_prod;
630 rmb(); /* Ensure we see queued responses up to 'rp'. */
631
632 for (i = info->ring.rsp_cons; i != rp; i++) {
633 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700634
635 bret = RING_GET_RESPONSE(&info->ring, i);
636 id = bret->id;
637 req = (struct request *)info->shadow[id].request;
638
639 blkif_completion(&info->shadow[id]);
640
641 add_id_to_freelist(info, id);
642
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500643 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700644 switch (bret->operation) {
645 case BLKIF_OP_WRITE_BARRIER:
646 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
647 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
648 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500649 error = -EOPNOTSUPP;
Tejun Heo4913efe2010-09-03 11:56:16 +0200650 info->feature_flush = 0;
651 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700652 }
653 /* fall through */
654 case BLKIF_OP_READ:
655 case BLKIF_OP_WRITE:
656 if (unlikely(bret->status != BLKIF_RSP_OKAY))
657 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
658 "request: %x\n", bret->status);
659
Tejun Heo40cbbb72009-04-23 11:05:19 +0900660 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700661 break;
662 default:
663 BUG();
664 }
665 }
666
667 info->ring.rsp_cons = i;
668
669 if (i != info->ring.req_prod_pvt) {
670 int more_to_do;
671 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
672 if (more_to_do)
673 goto again;
674 } else
675 info->ring.sring->rsp_event = i + 1;
676
677 kick_pending_request_queues(info);
678
679 spin_unlock_irqrestore(&blkif_io_lock, flags);
680
681 return IRQ_HANDLED;
682}
683
684
685static int setup_blkring(struct xenbus_device *dev,
686 struct blkfront_info *info)
687{
688 struct blkif_sring *sring;
689 int err;
690
691 info->ring_ref = GRANT_INVALID_REF;
692
Ian Campbella144ff02008-06-17 10:47:08 +0200693 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700694 if (!sring) {
695 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
696 return -ENOMEM;
697 }
698 SHARED_RING_INIT(sring);
699 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
700
Jens Axboe9e973e62009-02-24 08:10:09 +0100701 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
702
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700703 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
704 if (err < 0) {
705 free_page((unsigned long)sring);
706 info->ring.sring = NULL;
707 goto fail;
708 }
709 info->ring_ref = err;
710
711 err = xenbus_alloc_evtchn(dev, &info->evtchn);
712 if (err)
713 goto fail;
714
715 err = bind_evtchn_to_irqhandler(info->evtchn,
716 blkif_interrupt,
717 IRQF_SAMPLE_RANDOM, "blkif", info);
718 if (err <= 0) {
719 xenbus_dev_fatal(dev, err,
720 "bind_evtchn_to_irqhandler failed");
721 goto fail;
722 }
723 info->irq = err;
724
725 return 0;
726fail:
727 blkif_free(info, 0);
728 return err;
729}
730
731
732/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000733static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700734 struct blkfront_info *info)
735{
736 const char *message = NULL;
737 struct xenbus_transaction xbt;
738 int err;
739
740 /* Create shared ring, alloc event channel. */
741 err = setup_blkring(dev, info);
742 if (err)
743 goto out;
744
745again:
746 err = xenbus_transaction_start(&xbt);
747 if (err) {
748 xenbus_dev_fatal(dev, err, "starting transaction");
749 goto destroy_blkring;
750 }
751
752 err = xenbus_printf(xbt, dev->nodename,
753 "ring-ref", "%u", info->ring_ref);
754 if (err) {
755 message = "writing ring-ref";
756 goto abort_transaction;
757 }
758 err = xenbus_printf(xbt, dev->nodename,
759 "event-channel", "%u", info->evtchn);
760 if (err) {
761 message = "writing event-channel";
762 goto abort_transaction;
763 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700764 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
765 XEN_IO_PROTO_ABI_NATIVE);
766 if (err) {
767 message = "writing protocol";
768 goto abort_transaction;
769 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700770
771 err = xenbus_transaction_end(xbt, 0);
772 if (err) {
773 if (err == -EAGAIN)
774 goto again;
775 xenbus_dev_fatal(dev, err, "completing transaction");
776 goto destroy_blkring;
777 }
778
779 xenbus_switch_state(dev, XenbusStateInitialised);
780
781 return 0;
782
783 abort_transaction:
784 xenbus_transaction_end(xbt, 1);
785 if (message)
786 xenbus_dev_fatal(dev, err, "%s", message);
787 destroy_blkring:
788 blkif_free(info, 0);
789 out:
790 return err;
791}
792
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700793/**
794 * Entry point to this code when a new device is created. Allocate the basic
795 * structures and the ring buffer for communication with the backend, and
796 * inform the backend of the appropriate details for those. Switch to
797 * Initialised state.
798 */
799static int blkfront_probe(struct xenbus_device *dev,
800 const struct xenbus_device_id *id)
801{
802 int err, vdevice, i;
803 struct blkfront_info *info;
804
805 /* FIXME: Use dynamic device id if this is not set. */
806 err = xenbus_scanf(XBT_NIL, dev->nodename,
807 "virtual-device", "%i", &vdevice);
808 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700809 /* go looking in the extended area instead */
810 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
811 "%i", &vdevice);
812 if (err != 1) {
813 xenbus_dev_fatal(dev, err, "reading virtual-device");
814 return err;
815 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700816 }
817
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100818 if (xen_hvm_domain()) {
819 char *type;
820 int len;
821 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100822 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100823 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100824
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100825 if (!VDEV_IS_EXTENDED(vdevice))
826 major = BLKIF_MAJOR(vdevice);
827 else
828 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100829
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100830 if (major != XENVBD_MAJOR) {
831 printk(KERN_INFO
832 "%s: HVM does not support vbd %d as xen block device\n",
833 __FUNCTION__, vdevice);
834 return -ENODEV;
835 }
836 }
837 /* do not create a PV cdrom device if we are an HVM guest */
838 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
839 if (IS_ERR(type))
840 return -ENODEV;
841 if (strncmp(type, "cdrom", 5) == 0) {
842 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100843 return -ENODEV;
844 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100845 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100846 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700847 info = kzalloc(sizeof(*info), GFP_KERNEL);
848 if (!info) {
849 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
850 return -ENOMEM;
851 }
852
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000853 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700854 info->xbdev = dev;
855 info->vdevice = vdevice;
856 info->connected = BLKIF_STATE_DISCONNECTED;
857 INIT_WORK(&info->work, blkif_restart_queue);
858
859 for (i = 0; i < BLK_RING_SIZE; i++)
860 info->shadow[i].req.id = i+1;
861 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
862
863 /* Front end dir is a number, which is used as the id. */
864 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700865 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700866
Ian Campbell203fd612009-12-04 15:33:54 +0000867 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700868 if (err) {
869 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700870 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700871 return err;
872 }
873
874 return 0;
875}
876
877
878static int blkif_recover(struct blkfront_info *info)
879{
880 int i;
881 struct blkif_request *req;
882 struct blk_shadow *copy;
883 int j;
884
885 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200886 copy = kmalloc(sizeof(info->shadow),
887 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700888 if (!copy)
889 return -ENOMEM;
890 memcpy(copy, info->shadow, sizeof(info->shadow));
891
892 /* Stage 2: Set up free list. */
893 memset(&info->shadow, 0, sizeof(info->shadow));
894 for (i = 0; i < BLK_RING_SIZE; i++)
895 info->shadow[i].req.id = i+1;
896 info->shadow_free = info->ring.req_prod_pvt;
897 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
898
899 /* Stage 3: Find pending requests and requeue them. */
900 for (i = 0; i < BLK_RING_SIZE; i++) {
901 /* Not in use? */
902 if (copy[i].request == 0)
903 continue;
904
905 /* Grab a request slot and copy shadow state into it. */
906 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
907 *req = copy[i].req;
908
909 /* We get a new request id, and must reset the shadow state. */
910 req->id = get_id_from_freelist(info);
911 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
912
913 /* Rewrite any grant references invalidated by susp/resume. */
914 for (j = 0; j < req->nr_segments; j++)
915 gnttab_grant_foreign_access_ref(
916 req->seg[j].gref,
917 info->xbdev->otherend_id,
918 pfn_to_mfn(info->shadow[req->id].frame[j]),
919 rq_data_dir(
920 (struct request *)
921 info->shadow[req->id].request));
922 info->shadow[req->id].req = *req;
923
924 info->ring.req_prod_pvt++;
925 }
926
927 kfree(copy);
928
929 xenbus_switch_state(info->xbdev, XenbusStateConnected);
930
931 spin_lock_irq(&blkif_io_lock);
932
933 /* Now safe for us to use the shared ring */
934 info->connected = BLKIF_STATE_CONNECTED;
935
936 /* Send off requeued requests */
937 flush_requests(info);
938
939 /* Kick any other new requests queued since we resumed */
940 kick_pending_request_queues(info);
941
942 spin_unlock_irq(&blkif_io_lock);
943
944 return 0;
945}
946
947/**
948 * We are reconnecting to the backend, due to a suspend/resume, or a backend
949 * driver restart. We tear down our blkif structure and recreate it, but
950 * leave the device-layer structures intact so that this is transparent to the
951 * rest of the kernel.
952 */
953static int blkfront_resume(struct xenbus_device *dev)
954{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700955 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700956 int err;
957
958 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
959
960 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
961
Ian Campbell203fd612009-12-04 15:33:54 +0000962 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700963 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
964 err = blkif_recover(info);
965
966 return err;
967}
968
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000969static void
970blkfront_closing(struct blkfront_info *info)
971{
972 struct xenbus_device *xbdev = info->xbdev;
973 struct block_device *bdev = NULL;
974
975 mutex_lock(&info->mutex);
976
977 if (xbdev->state == XenbusStateClosing) {
978 mutex_unlock(&info->mutex);
979 return;
980 }
981
982 if (info->gd)
983 bdev = bdget_disk(info->gd, 0);
984
985 mutex_unlock(&info->mutex);
986
987 if (!bdev) {
988 xenbus_frontend_closed(xbdev);
989 return;
990 }
991
992 mutex_lock(&bdev->bd_mutex);
993
Daniel Stodden7b32d102010-04-30 22:01:23 +0000994 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000995 xenbus_dev_error(xbdev, -EBUSY,
996 "Device in use; refusing to close");
997 xenbus_switch_state(xbdev, XenbusStateClosing);
998 } else {
999 xlvbd_release_gendisk(info);
1000 xenbus_frontend_closed(xbdev);
1001 }
1002
1003 mutex_unlock(&bdev->bd_mutex);
1004 bdput(bdev);
1005}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001006
1007/*
1008 * Invoked when the backend is finally 'ready' (and has told produced
1009 * the details about the physical device - #sectors, size, etc).
1010 */
1011static void blkfront_connect(struct blkfront_info *info)
1012{
1013 unsigned long long sectors;
1014 unsigned long sector_size;
1015 unsigned int binfo;
1016 int err;
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001017 int barrier;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001018
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001019 switch (info->connected) {
1020 case BLKIF_STATE_CONNECTED:
1021 /*
1022 * Potentially, the back-end may be signalling
1023 * a capacity change; update the capacity.
1024 */
1025 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1026 "sectors", "%Lu", &sectors);
1027 if (XENBUS_EXIST_ERR(err))
1028 return;
1029 printk(KERN_INFO "Setting capacity to %Lu\n",
1030 sectors);
1031 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001032 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001033
1034 /* fall through */
1035 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001036 return;
1037
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001038 default:
1039 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001040 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001041
1042 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1043 __func__, info->xbdev->otherend);
1044
1045 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1046 "sectors", "%llu", &sectors,
1047 "info", "%u", &binfo,
1048 "sector-size", "%lu", &sector_size,
1049 NULL);
1050 if (err) {
1051 xenbus_dev_fatal(info->xbdev, err,
1052 "reading backend fields at %s",
1053 info->xbdev->otherend);
1054 return;
1055 }
1056
1057 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001058 "feature-barrier", "%lu", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001059 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001060
1061 /*
1062 * If there's no "feature-barrier" defined, then it means
1063 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001064 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001065 *
Tejun Heo6958f142010-09-03 11:56:16 +02001066 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001067 */
Tejun Heo4913efe2010-09-03 11:56:16 +02001068 info->feature_flush = 0;
Jens Axboe005a1d12010-10-22 10:58:33 +02001069
1070 /*
1071 * The driver doesn't properly handled empty flushes, so
1072 * lets disable barrier support for now.
1073 */
1074#if 0
Tejun Heo4913efe2010-09-03 11:56:16 +02001075 if (!err && barrier)
1076 info->feature_flush = REQ_FLUSH;
Jens Axboe005a1d12010-10-22 10:58:33 +02001077#endif
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001078
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001079 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001080 if (err) {
1081 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1082 info->xbdev->otherend);
1083 return;
1084 }
1085
1086 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1087
1088 /* Kick pending requests. */
1089 spin_lock_irq(&blkif_io_lock);
1090 info->connected = BLKIF_STATE_CONNECTED;
1091 kick_pending_request_queues(info);
1092 spin_unlock_irq(&blkif_io_lock);
1093
1094 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001095
1096 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001097}
1098
1099/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001100 * Callback received when the backend's state changes.
1101 */
Ian Campbell203fd612009-12-04 15:33:54 +00001102static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001103 enum xenbus_state backend_state)
1104{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001105 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001106
Ian Campbell203fd612009-12-04 15:33:54 +00001107 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001108
1109 switch (backend_state) {
1110 case XenbusStateInitialising:
1111 case XenbusStateInitWait:
1112 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001113 case XenbusStateReconfiguring:
1114 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001115 case XenbusStateUnknown:
1116 case XenbusStateClosed:
1117 break;
1118
1119 case XenbusStateConnected:
1120 blkfront_connect(info);
1121 break;
1122
1123 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001124 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001125 break;
1126 }
1127}
1128
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001129static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001130{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001131 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1132 struct block_device *bdev = NULL;
1133 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001134
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001135 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001136
1137 blkif_free(info, 0);
1138
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001139 mutex_lock(&info->mutex);
1140
1141 disk = info->gd;
1142 if (disk)
1143 bdev = bdget_disk(disk, 0);
1144
1145 info->xbdev = NULL;
1146 mutex_unlock(&info->mutex);
1147
1148 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001149 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001150 return 0;
1151 }
1152
1153 /*
1154 * The xbdev was removed before we reached the Closed
1155 * state. See if it's safe to remove the disk. If the bdev
1156 * isn't closed yet, we let release take care of it.
1157 */
1158
1159 mutex_lock(&bdev->bd_mutex);
1160 info = disk->private_data;
1161
Daniel Stoddend54142c2010-08-07 18:51:21 +02001162 dev_warn(disk_to_dev(disk),
1163 "%s was hot-unplugged, %d stale handles\n",
1164 xbdev->nodename, bdev->bd_openers);
1165
Daniel Stodden7b32d102010-04-30 22:01:23 +00001166 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001167 xlvbd_release_gendisk(info);
1168 disk->private_data = NULL;
1169 kfree(info);
1170 }
1171
1172 mutex_unlock(&bdev->bd_mutex);
1173 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001174
1175 return 0;
1176}
1177
Christian Limpach1d78d702008-04-02 10:54:04 -07001178static int blkfront_is_ready(struct xenbus_device *dev)
1179{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001180 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001181
Jan Beulich5d7ed202010-08-07 18:31:12 +02001182 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001183}
1184
Al Viroa63c8482008-03-02 10:23:47 -05001185static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001186{
Daniel Stodden13961742010-08-07 18:36:53 +02001187 struct gendisk *disk = bdev->bd_disk;
1188 struct blkfront_info *info;
1189 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001190
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001191 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001192
Daniel Stodden13961742010-08-07 18:36:53 +02001193 info = disk->private_data;
1194 if (!info) {
1195 /* xbdev gone */
1196 err = -ERESTARTSYS;
1197 goto out;
1198 }
1199
1200 mutex_lock(&info->mutex);
1201
1202 if (!info->gd)
1203 /* xbdev is closed */
1204 err = -ERESTARTSYS;
1205
1206 mutex_unlock(&info->mutex);
1207
Daniel Stodden13961742010-08-07 18:36:53 +02001208out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001209 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001210 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001211}
1212
Al Viroa63c8482008-03-02 10:23:47 -05001213static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001214{
Al Viroa63c8482008-03-02 10:23:47 -05001215 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001216 struct block_device *bdev;
1217 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001218
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001219 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001220
1221 bdev = bdget_disk(disk, 0);
1222 bdput(bdev);
1223
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001224 if (bdev->bd_openers)
1225 goto out;
1226
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001227 /*
1228 * Check if we have been instructed to close. We will have
1229 * deferred this request, because the bdev was still open.
1230 */
1231
1232 mutex_lock(&info->mutex);
1233 xbdev = info->xbdev;
1234
1235 if (xbdev && xbdev->state == XenbusStateClosing) {
1236 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001237 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001238 xlvbd_release_gendisk(info);
1239 xenbus_frontend_closed(info->xbdev);
1240 }
1241
1242 mutex_unlock(&info->mutex);
1243
1244 if (!xbdev) {
1245 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001246 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001247 xlvbd_release_gendisk(info);
1248 disk->private_data = NULL;
1249 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001250 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001251
Jens Axboea4cc14e2010-08-08 21:50:05 -04001252out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001253 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001254 return 0;
1255}
1256
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001257static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001258{
1259 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001260 .open = blkif_open,
1261 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001262 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001263 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001264};
1265
1266
Márton Némethec9c42e2010-01-10 13:39:52 +01001267static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001268 { "vbd" },
1269 { "" }
1270};
1271
1272static struct xenbus_driver blkfront = {
1273 .name = "vbd",
1274 .owner = THIS_MODULE,
1275 .ids = blkfront_ids,
1276 .probe = blkfront_probe,
1277 .remove = blkfront_remove,
1278 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001279 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001280 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001281};
1282
1283static int __init xlblk_init(void)
1284{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001285 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001286 return -ENODEV;
1287
1288 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1289 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1290 XENVBD_MAJOR, DEV_NAME);
1291 return -ENODEV;
1292 }
1293
1294 return xenbus_register_frontend(&blkfront);
1295}
1296module_init(xlblk_init);
1297
1298
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001299static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001300{
1301 return xenbus_unregister_driver(&blkfront);
1302}
1303module_exit(xlblk_exit);
1304
1305MODULE_DESCRIPTION("Xen virtual block device frontend");
1306MODULE_LICENSE("GPL");
1307MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001308MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f092008-04-02 10:54:06 -07001309MODULE_ALIAS("xenblk");