Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 40 | #include <linux/hdreg.h> |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 41 | #include <linux/cdrom.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 42 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 44 | #include <linux/mutex.h> |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 45 | #include <linux/scatterlist.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 47 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 48 | #include <xen/xenbus.h> |
| 49 | #include <xen/grant_table.h> |
| 50 | #include <xen/events.h> |
| 51 | #include <xen/page.h> |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 52 | #include <xen/platform_pci.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 53 | |
| 54 | #include <xen/interface/grant_table.h> |
| 55 | #include <xen/interface/io/blkif.h> |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 56 | #include <xen/interface/io/protocols.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 57 | |
| 58 | #include <asm/xen/hypervisor.h> |
| 59 | |
| 60 | enum blkif_state { |
| 61 | BLKIF_STATE_DISCONNECTED, |
| 62 | BLKIF_STATE_CONNECTED, |
| 63 | BLKIF_STATE_SUSPENDED, |
| 64 | }; |
| 65 | |
| 66 | struct blk_shadow { |
| 67 | struct blkif_request req; |
| 68 | unsigned long request; |
| 69 | unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
| 70 | }; |
| 71 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 72 | static DEFINE_MUTEX(blkfront_mutex); |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 73 | static const struct block_device_operations xlvbd_block_fops; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 74 | |
| 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 | */ |
| 82 | struct blkfront_info |
| 83 | { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 84 | struct mutex mutex; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 85 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 86 | 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 Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 92 | struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 93 | 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 Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 99 | unsigned int feature_flush; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 100 | int is_ready; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static DEFINE_SPINLOCK(blkif_io_lock); |
| 104 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 105 | static unsigned int nr_minors; |
| 106 | static unsigned long *minors; |
| 107 | static DEFINE_SPINLOCK(minor_lock); |
| 108 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 109 | #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 Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 114 | #define PARTS_PER_EXT_DISK 256 |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 115 | |
| 116 | #define BLKIF_MAJOR(dev) ((dev)>>8) |
| 117 | #define BLKIF_MINOR(dev) ((dev) & 0xff) |
| 118 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 119 | #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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 123 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 124 | #define DEV_NAME "xvd" /* name in /dev */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 125 | |
| 126 | static int get_id_from_freelist(struct blkfront_info *info) |
| 127 | { |
| 128 | unsigned long free = info->shadow_free; |
Roel Kluin | b9ed725 | 2009-05-22 09:25:32 +0200 | [diff] [blame] | 129 | BUG_ON(free >= BLK_RING_SIZE); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 130 | info->shadow_free = info->shadow[free].req.id; |
| 131 | info->shadow[free].req.id = 0x0fffffee; /* debug */ |
| 132 | return free; |
| 133 | } |
| 134 | |
| 135 | static 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 Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 143 | static 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 | |
| 181 | static 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 192 | static 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 Harrison | afe42d7 | 2008-04-29 00:59:47 -0700 | [diff] [blame] | 198 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 199 | { |
| 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 Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 214 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
Adrian Bunk | 62aa005 | 2008-08-04 11:59:05 +0200 | [diff] [blame] | 215 | unsigned command, unsigned long argument) |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 216 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 217 | struct blkfront_info *info = bdev->bd_disk->private_data; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 218 | 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 247 | /* |
| 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 | */ |
| 257 | static 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 262 | unsigned long id; |
| 263 | unsigned int fsect, lsect; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 264 | int i, ref; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 265 | grant_ref_t gref_head; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 266 | struct scatterlist *sg; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 267 | |
| 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 Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 287 | ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 288 | ring_req->handle = info->handle; |
| 289 | |
| 290 | ring_req->operation = rq_data_dir(req) ? |
| 291 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 292 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 293 | 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 Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 300 | /* install a grant reference. */ |
| 301 | ref = gnttab_claim_grant_reference(&gref_head); |
| 302 | BUG_ON(ref == -ENOSPC); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 303 | |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 304 | gnttab_grant_foreign_access_ref( |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 305 | ref, |
| 306 | info->xbdev->otherend_id, |
| 307 | buffer_mfn, |
| 308 | rq_data_dir(req) ); |
| 309 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 310 | info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn); |
| 311 | ring_req->seg[i] = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 312 | (struct blkif_request_segment) { |
| 313 | .gref = ref, |
| 314 | .first_sect = fsect, |
| 315 | .last_sect = lsect }; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 316 | } |
| 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 | |
| 329 | static 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 Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 343 | static void do_blkif_request(struct request_queue *rq) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 344 | { |
| 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 Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 353 | while ((req = blk_peek_request(rq)) != NULL) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 354 | info = req->rq_disk->private_data; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 355 | |
| 356 | if (RING_FULL(&info->ring)) |
| 357 | goto wait; |
| 358 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 359 | blk_start_request(req); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 360 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 361 | if (req->cmd_type != REQ_TYPE_FS) { |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 362 | __blk_end_request_all(req, -EIO); |
| 363 | continue; |
| 364 | } |
| 365 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 366 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 367 | "(%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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 371 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 372 | if (blkif_queue_request(req)) { |
| 373 | blk_requeue_request(rq, req); |
| 374 | wait: |
| 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 | |
| 387 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) |
| 388 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 389 | struct request_queue *rq; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 390 | |
| 391 | rq = blk_init_queue(do_blkif_request, &blkif_io_lock); |
| 392 | if (rq == NULL) |
| 393 | return -1; |
| 394 | |
Fernando Luis Vázquez Cao | 66d352e | 2008-10-27 18:45:54 +0900 | [diff] [blame] | 395 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 396 | |
| 397 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 398 | blk_queue_logical_block_size(rq, sector_size); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 399 | blk_queue_max_hw_sectors(rq, 512); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 400 | |
| 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. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 406 | blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 407 | |
| 408 | /* Make sure buffer addresses are sector-aligned. */ |
| 409 | blk_queue_dma_alignment(rq, 511); |
| 410 | |
Ian Campbell | 1c91fe1 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 411 | /* Make sure we don't use bounce buffers. */ |
| 412 | blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY); |
| 413 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 414 | gd->queue = rq; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 420 | static void xlvbd_flush(struct blkfront_info *info) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 421 | { |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 422 | blk_queue_flush(info->rq, info->feature_flush); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 423 | printk(KERN_INFO "blkfront: %s: barriers %s\n", |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 424 | info->gd->disk_name, |
| 425 | info->feature_flush ? "enabled" : "disabled"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 429 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
| 430 | struct blkfront_info *info, |
| 431 | u16 vdisk_info, u16 sector_size) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 432 | { |
| 433 | struct gendisk *gd; |
| 434 | int nr_minors = 1; |
| 435 | int err = -ENODEV; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 436 | unsigned int offset; |
| 437 | int minor; |
| 438 | int nr_parts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 439 | |
| 440 | BUG_ON(info->gd != NULL); |
| 441 | BUG_ON(info->rq != NULL); |
| 442 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 443 | 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 459 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 460 | err = xlbd_reserve_minors(minor, nr_minors); |
| 461 | if (err) |
| 462 | goto out; |
| 463 | err = -ENODEV; |
| 464 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 465 | gd = alloc_disk(nr_minors); |
| 466 | if (gd == NULL) |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 467 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 468 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 469 | 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 488 | |
| 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 Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 498 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | info->rq = gd->queue; |
| 502 | info->gd = gd; |
| 503 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 504 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 505 | |
| 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 Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 517 | release: |
| 518 | xlbd_release_minors(minor, nr_minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 519 | out: |
| 520 | return err; |
| 521 | } |
| 522 | |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 523 | static 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 556 | static 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 | |
| 566 | static 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 | |
| 576 | static 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 | |
| 605 | static 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 | |
| 612 | static 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 Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 619 | int error; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 620 | |
| 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 634 | |
| 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 Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 643 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 644 | 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 Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 649 | error = -EOPNOTSUPP; |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 650 | info->feature_flush = 0; |
| 651 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 652 | } |
| 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 Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 660 | __blk_end_request_all(req, error); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 661 | 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 | |
| 685 | static 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 Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 693 | sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 694 | 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 Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 701 | sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 702 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 703 | 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; |
| 726 | fail: |
| 727 | blkif_free(info, 0); |
| 728 | return err; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 733 | static int talk_to_blkback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 734 | 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 | |
| 745 | again: |
| 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 Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 764 | 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 770 | |
| 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 793 | /** |
| 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 | */ |
| 799 | static 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 Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 809 | /* 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 818 | if (xen_hvm_domain()) { |
| 819 | char *type; |
| 820 | int len; |
| 821 | /* no unplug has been done: do not hook devices != xen vbds */ |
Ian Campbell | 1dc7ce9 | 2010-08-23 11:59:29 +0100 | [diff] [blame] | 822 | if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) { |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 823 | int major; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 824 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 825 | if (!VDEV_IS_EXTENDED(vdevice)) |
| 826 | major = BLKIF_MAJOR(vdevice); |
| 827 | else |
| 828 | major = XENVBD_MAJOR; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 829 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 830 | 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 Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 843 | return -ENODEV; |
| 844 | } |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 845 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 846 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 847 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 848 | if (!info) { |
| 849 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); |
| 850 | return -ENOMEM; |
| 851 | } |
| 852 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 853 | mutex_init(&info->mutex); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 854 | 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-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 865 | dev_set_drvdata(&dev->dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 866 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 867 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 868 | if (err) { |
| 869 | kfree(info); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 870 | dev_set_drvdata(&dev->dev, NULL); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 871 | return err; |
| 872 | } |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | static 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 Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 886 | copy = kmalloc(sizeof(info->shadow), |
| 887 | GFP_NOIO | __GFP_REPEAT | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 888 | 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], ©[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 | */ |
| 953 | static int blkfront_resume(struct xenbus_device *dev) |
| 954 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 955 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 956 | 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 Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 962 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 963 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) |
| 964 | err = blkif_recover(info); |
| 965 | |
| 966 | return err; |
| 967 | } |
| 968 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 969 | static void |
| 970 | blkfront_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 Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 994 | if (bdev->bd_openers) { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 995 | 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 Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* |
| 1008 | * Invoked when the backend is finally 'ready' (and has told produced |
| 1009 | * the details about the physical device - #sectors, size, etc). |
| 1010 | */ |
| 1011 | static 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 Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1017 | int barrier; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1018 | |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1019 | 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", §ors); |
| 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. Srinivasan | 2def141 | 2010-03-18 15:00:54 -0700 | [diff] [blame] | 1032 | revalidate_disk(info->gd); |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1033 | |
| 1034 | /* fall through */ |
| 1035 | case BLKIF_STATE_SUSPENDED: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1036 | return; |
| 1037 | |
Jeremy Fitzhardinge | b4dddb4 | 2010-03-11 15:10:40 -0800 | [diff] [blame] | 1038 | default: |
| 1039 | break; |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1040 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1041 | |
| 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", §ors, |
| 1047 | "info", "%u", &binfo, |
| 1048 | "sector-size", "%lu", §or_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 Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1058 | "feature-barrier", "%lu", &barrier, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1059 | NULL); |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | * If there's no "feature-barrier" defined, then it means |
| 1063 | * we're dealing with a very old backend which writes |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1064 | * synchronously; nothing to do. |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1065 | * |
Tejun Heo | 6958f14 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1066 | * If there are barriers, then we use flush. |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1067 | */ |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1068 | info->feature_flush = 0; |
Jens Axboe | 005a1d1 | 2010-10-22 10:58:33 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * The driver doesn't properly handled empty flushes, so |
| 1072 | * lets disable barrier support for now. |
| 1073 | */ |
| 1074 | #if 0 |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1075 | if (!err && barrier) |
| 1076 | info->feature_flush = REQ_FLUSH; |
Jens Axboe | 005a1d1 | 2010-10-22 10:58:33 +0200 | [diff] [blame] | 1077 | #endif |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1078 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1079 | err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1080 | 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 Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1095 | |
| 1096 | info->is_ready = 1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /** |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1100 | * Callback received when the backend's state changes. |
| 1101 | */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1102 | static void blkback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1103 | enum xenbus_state backend_state) |
| 1104 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1105 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1106 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1107 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1108 | |
| 1109 | switch (backend_state) { |
| 1110 | case XenbusStateInitialising: |
| 1111 | case XenbusStateInitWait: |
| 1112 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 1113 | case XenbusStateReconfiguring: |
| 1114 | case XenbusStateReconfigured: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1115 | case XenbusStateUnknown: |
| 1116 | case XenbusStateClosed: |
| 1117 | break; |
| 1118 | |
| 1119 | case XenbusStateConnected: |
| 1120 | blkfront_connect(info); |
| 1121 | break; |
| 1122 | |
| 1123 | case XenbusStateClosing: |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1124 | blkfront_closing(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1125 | break; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1129 | static int blkfront_remove(struct xenbus_device *xbdev) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1130 | { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1131 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
| 1132 | struct block_device *bdev = NULL; |
| 1133 | struct gendisk *disk; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1134 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1135 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1136 | |
| 1137 | blkif_free(info, 0); |
| 1138 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1139 | 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 Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1149 | kfree(info); |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1150 | 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 Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1162 | dev_warn(disk_to_dev(disk), |
| 1163 | "%s was hot-unplugged, %d stale handles\n", |
| 1164 | xbdev->nodename, bdev->bd_openers); |
| 1165 | |
Daniel Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 1166 | if (info && !bdev->bd_openers) { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1167 | xlvbd_release_gendisk(info); |
| 1168 | disk->private_data = NULL; |
| 1169 | kfree(info); |
| 1170 | } |
| 1171 | |
| 1172 | mutex_unlock(&bdev->bd_mutex); |
| 1173 | bdput(bdev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1178 | static int blkfront_is_ready(struct xenbus_device *dev) |
| 1179 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1180 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1181 | |
Jan Beulich | 5d7ed20 | 2010-08-07 18:31:12 +0200 | [diff] [blame] | 1182 | return info->is_ready && info->xbdev; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1185 | static int blkif_open(struct block_device *bdev, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1186 | { |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1187 | struct gendisk *disk = bdev->bd_disk; |
| 1188 | struct blkfront_info *info; |
| 1189 | int err = 0; |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1190 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1191 | mutex_lock(&blkfront_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1192 | |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1193 | 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 Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1208 | out: |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1209 | mutex_unlock(&blkfront_mutex); |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1210 | return err; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1213 | static int blkif_release(struct gendisk *disk, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1214 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1215 | struct blkfront_info *info = disk->private_data; |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1216 | struct block_device *bdev; |
| 1217 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1218 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1219 | mutex_lock(&blkfront_mutex); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1220 | |
| 1221 | bdev = bdget_disk(disk, 0); |
| 1222 | bdput(bdev); |
| 1223 | |
Daniel Stodden | acfca3c | 2010-08-07 18:47:26 +0200 | [diff] [blame] | 1224 | if (bdev->bd_openers) |
| 1225 | goto out; |
| 1226 | |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1227 | /* |
| 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 Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1237 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1238 | 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 Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1246 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1247 | xlvbd_release_gendisk(info); |
| 1248 | disk->private_data = NULL; |
| 1249 | kfree(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1250 | } |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1251 | |
Jens Axboe | a4cc14e | 2010-08-08 21:50:05 -0400 | [diff] [blame] | 1252 | out: |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1253 | mutex_unlock(&blkfront_mutex); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1257 | static const struct block_device_operations xlvbd_block_fops = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1258 | { |
| 1259 | .owner = THIS_MODULE, |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1260 | .open = blkif_open, |
| 1261 | .release = blkif_release, |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 1262 | .getgeo = blkif_getgeo, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1263 | .ioctl = blkif_ioctl, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1264 | }; |
| 1265 | |
| 1266 | |
Márton Németh | ec9c42e | 2010-01-10 13:39:52 +0100 | [diff] [blame] | 1267 | static const struct xenbus_device_id blkfront_ids[] = { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1268 | { "vbd" }, |
| 1269 | { "" } |
| 1270 | }; |
| 1271 | |
| 1272 | static 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 Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1279 | .otherend_changed = blkback_changed, |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1280 | .is_ready = blkfront_is_ready, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1281 | }; |
| 1282 | |
| 1283 | static int __init xlblk_init(void) |
| 1284 | { |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 1285 | if (!xen_domain()) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1286 | 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 | } |
| 1296 | module_init(xlblk_init); |
| 1297 | |
| 1298 | |
Jan Beulich | 5a60d0c | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1299 | static void __exit xlblk_exit(void) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1300 | { |
| 1301 | return xenbus_unregister_driver(&blkfront); |
| 1302 | } |
| 1303 | module_exit(xlblk_exit); |
| 1304 | |
| 1305 | MODULE_DESCRIPTION("Xen virtual block device frontend"); |
| 1306 | MODULE_LICENSE("GPL"); |
| 1307 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 1308 | MODULE_ALIAS("xen:vbd"); |
Mark McLoughlin | 4f93f09 | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 1309 | MODULE_ALIAS("xenblk"); |