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 | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 44 | #include <linux/smp_lock.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 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 72 | static const struct block_device_operations xlvbd_block_fops; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 73 | |
| 74 | #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE) |
| 75 | |
| 76 | /* |
| 77 | * We have one of these per vbd, whether ide, scsi or 'other'. They |
| 78 | * hang in private_data off the gendisk structure. We may end up |
| 79 | * putting all kinds of interesting stuff here :-) |
| 80 | */ |
| 81 | struct blkfront_info |
| 82 | { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 83 | struct mutex mutex; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 84 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 85 | struct gendisk *gd; |
| 86 | int vdevice; |
| 87 | blkif_vdev_t handle; |
| 88 | enum blkif_state connected; |
| 89 | int ring_ref; |
| 90 | struct blkif_front_ring ring; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 91 | struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 92 | unsigned int evtchn, irq; |
| 93 | struct request_queue *rq; |
| 94 | struct work_struct work; |
| 95 | struct gnttab_free_callback callback; |
| 96 | struct blk_shadow shadow[BLK_RING_SIZE]; |
| 97 | unsigned long shadow_free; |
| 98 | int feature_barrier; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 99 | int is_ready; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static DEFINE_SPINLOCK(blkif_io_lock); |
| 103 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 104 | static unsigned int nr_minors; |
| 105 | static unsigned long *minors; |
| 106 | static DEFINE_SPINLOCK(minor_lock); |
| 107 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 108 | #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ |
| 109 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) |
| 110 | #define GRANT_INVALID_REF 0 |
| 111 | |
| 112 | #define PARTS_PER_DISK 16 |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 113 | #define PARTS_PER_EXT_DISK 256 |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 114 | |
| 115 | #define BLKIF_MAJOR(dev) ((dev)>>8) |
| 116 | #define BLKIF_MINOR(dev) ((dev) & 0xff) |
| 117 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 118 | #define EXT_SHIFT 28 |
| 119 | #define EXTENDED (1<<EXT_SHIFT) |
| 120 | #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) |
| 121 | #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 122 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 123 | #define DEV_NAME "xvd" /* name in /dev */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 124 | |
| 125 | static int get_id_from_freelist(struct blkfront_info *info) |
| 126 | { |
| 127 | unsigned long free = info->shadow_free; |
Roel Kluin | b9ed725 | 2009-05-22 09:25:32 +0200 | [diff] [blame] | 128 | BUG_ON(free >= BLK_RING_SIZE); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 129 | info->shadow_free = info->shadow[free].req.id; |
| 130 | info->shadow[free].req.id = 0x0fffffee; /* debug */ |
| 131 | return free; |
| 132 | } |
| 133 | |
| 134 | static void add_id_to_freelist(struct blkfront_info *info, |
| 135 | unsigned long id) |
| 136 | { |
| 137 | info->shadow[id].req.id = info->shadow_free; |
| 138 | info->shadow[id].request = 0; |
| 139 | info->shadow_free = id; |
| 140 | } |
| 141 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 142 | static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) |
| 143 | { |
| 144 | unsigned int end = minor + nr; |
| 145 | int rc; |
| 146 | |
| 147 | if (end > nr_minors) { |
| 148 | unsigned long *bitmap, *old; |
| 149 | |
| 150 | bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap), |
| 151 | GFP_KERNEL); |
| 152 | if (bitmap == NULL) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | spin_lock(&minor_lock); |
| 156 | if (end > nr_minors) { |
| 157 | old = minors; |
| 158 | memcpy(bitmap, minors, |
| 159 | BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); |
| 160 | minors = bitmap; |
| 161 | nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; |
| 162 | } else |
| 163 | old = bitmap; |
| 164 | spin_unlock(&minor_lock); |
| 165 | kfree(old); |
| 166 | } |
| 167 | |
| 168 | spin_lock(&minor_lock); |
| 169 | if (find_next_bit(minors, end, minor) >= end) { |
| 170 | for (; minor < end; ++minor) |
| 171 | __set_bit(minor, minors); |
| 172 | rc = 0; |
| 173 | } else |
| 174 | rc = -EBUSY; |
| 175 | spin_unlock(&minor_lock); |
| 176 | |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | static void xlbd_release_minors(unsigned int minor, unsigned int nr) |
| 181 | { |
| 182 | unsigned int end = minor + nr; |
| 183 | |
| 184 | BUG_ON(end > nr_minors); |
| 185 | spin_lock(&minor_lock); |
| 186 | for (; minor < end; ++minor) |
| 187 | __clear_bit(minor, minors); |
| 188 | spin_unlock(&minor_lock); |
| 189 | } |
| 190 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 191 | static void blkif_restart_queue_callback(void *arg) |
| 192 | { |
| 193 | struct blkfront_info *info = (struct blkfront_info *)arg; |
| 194 | schedule_work(&info->work); |
| 195 | } |
| 196 | |
Harvey Harrison | afe42d7 | 2008-04-29 00:59:47 -0700 | [diff] [blame] | 197 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 198 | { |
| 199 | /* We don't have real geometry info, but let's at least return |
| 200 | values consistent with the size of the device */ |
| 201 | sector_t nsect = get_capacity(bd->bd_disk); |
| 202 | sector_t cylinders = nsect; |
| 203 | |
| 204 | hg->heads = 0xff; |
| 205 | hg->sectors = 0x3f; |
| 206 | sector_div(cylinders, hg->heads * hg->sectors); |
| 207 | hg->cylinders = cylinders; |
| 208 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) |
| 209 | hg->cylinders = 0xffff; |
| 210 | return 0; |
| 211 | } |
| 212 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 213 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
Adrian Bunk | 62aa005 | 2008-08-04 11:59:05 +0200 | [diff] [blame] | 214 | unsigned command, unsigned long argument) |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 215 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 216 | struct blkfront_info *info = bdev->bd_disk->private_data; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 217 | int i; |
| 218 | |
| 219 | dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n", |
| 220 | command, (long)argument); |
| 221 | |
| 222 | switch (command) { |
| 223 | case CDROMMULTISESSION: |
| 224 | dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n"); |
| 225 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) |
| 226 | if (put_user(0, (char __user *)(argument + i))) |
| 227 | return -EFAULT; |
| 228 | return 0; |
| 229 | |
| 230 | case CDROM_GET_CAPABILITY: { |
| 231 | struct gendisk *gd = info->gd; |
| 232 | if (gd->flags & GENHD_FL_CD) |
| 233 | return 0; |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | |
| 237 | default: |
| 238 | /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", |
| 239 | command);*/ |
| 240 | return -EINVAL; /* same return as native Linux */ |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 246 | /* |
| 247 | * blkif_queue_request |
| 248 | * |
| 249 | * request block io |
| 250 | * |
| 251 | * id: for guest use only. |
| 252 | * operation: BLKIF_OP_{READ,WRITE,PROBE} |
| 253 | * buffer: buffer to read/write into. this should be a |
| 254 | * virtual address in the guest os. |
| 255 | */ |
| 256 | static int blkif_queue_request(struct request *req) |
| 257 | { |
| 258 | struct blkfront_info *info = req->rq_disk->private_data; |
| 259 | unsigned long buffer_mfn; |
| 260 | struct blkif_request *ring_req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 261 | unsigned long id; |
| 262 | unsigned int fsect, lsect; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 263 | int i, ref; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 264 | grant_ref_t gref_head; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 265 | struct scatterlist *sg; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 266 | |
| 267 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) |
| 268 | return 1; |
| 269 | |
| 270 | if (gnttab_alloc_grant_references( |
| 271 | BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) { |
| 272 | gnttab_request_free_callback( |
| 273 | &info->callback, |
| 274 | blkif_restart_queue_callback, |
| 275 | info, |
| 276 | BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | /* Fill out a communications ring structure. */ |
| 281 | ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); |
| 282 | id = get_id_from_freelist(info); |
| 283 | info->shadow[id].request = (unsigned long)req; |
| 284 | |
| 285 | ring_req->id = id; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 286 | ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 287 | ring_req->handle = info->handle; |
| 288 | |
| 289 | ring_req->operation = rq_data_dir(req) ? |
| 290 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 291 | if (req->cmd_flags & REQ_HARDBARRIER) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 292 | ring_req->operation = BLKIF_OP_WRITE_BARRIER; |
| 293 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 294 | ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg); |
| 295 | BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 296 | |
| 297 | for_each_sg(info->sg, sg, ring_req->nr_segments, i) { |
| 298 | buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg))); |
| 299 | fsect = sg->offset >> 9; |
| 300 | lsect = fsect + (sg->length >> 9) - 1; |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 301 | /* install a grant reference. */ |
| 302 | ref = gnttab_claim_grant_reference(&gref_head); |
| 303 | BUG_ON(ref == -ENOSPC); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 304 | |
Jens Axboe | 6c92e69 | 2007-08-16 13:43:12 +0200 | [diff] [blame] | 305 | gnttab_grant_foreign_access_ref( |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 306 | ref, |
| 307 | info->xbdev->otherend_id, |
| 308 | buffer_mfn, |
| 309 | rq_data_dir(req) ); |
| 310 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 311 | info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn); |
| 312 | ring_req->seg[i] = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 313 | (struct blkif_request_segment) { |
| 314 | .gref = ref, |
| 315 | .first_sect = fsect, |
| 316 | .last_sect = lsect }; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | info->ring.req_prod_pvt++; |
| 320 | |
| 321 | /* Keep a private copy so we can reissue requests when recovering. */ |
| 322 | info->shadow[id].req = *ring_req; |
| 323 | |
| 324 | gnttab_free_grant_references(gref_head); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | static inline void flush_requests(struct blkfront_info *info) |
| 331 | { |
| 332 | int notify; |
| 333 | |
| 334 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); |
| 335 | |
| 336 | if (notify) |
| 337 | notify_remote_via_irq(info->irq); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * do_blkif_request |
| 342 | * read a block; request is in a request queue |
| 343 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 344 | static void do_blkif_request(struct request_queue *rq) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 345 | { |
| 346 | struct blkfront_info *info = NULL; |
| 347 | struct request *req; |
| 348 | int queued; |
| 349 | |
| 350 | pr_debug("Entered do_blkif_request\n"); |
| 351 | |
| 352 | queued = 0; |
| 353 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 354 | while ((req = blk_peek_request(rq)) != NULL) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 355 | info = req->rq_disk->private_data; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 356 | |
| 357 | if (RING_FULL(&info->ring)) |
| 358 | goto wait; |
| 359 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 360 | blk_start_request(req); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 361 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 362 | if (req->cmd_type != REQ_TYPE_FS) { |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 363 | __blk_end_request_all(req, -EIO); |
| 364 | continue; |
| 365 | } |
| 366 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 367 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 368 | "(%u/%u) buffer:%p [%s]\n", |
| 369 | req, req->cmd, (unsigned long)blk_rq_pos(req), |
| 370 | blk_rq_cur_sectors(req), blk_rq_sectors(req), |
| 371 | req->buffer, rq_data_dir(req) ? "write" : "read"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 372 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 373 | if (blkif_queue_request(req)) { |
| 374 | blk_requeue_request(rq, req); |
| 375 | wait: |
| 376 | /* Avoid pointless unplugs. */ |
| 377 | blk_stop_queue(rq); |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | queued++; |
| 382 | } |
| 383 | |
| 384 | if (queued != 0) |
| 385 | flush_requests(info); |
| 386 | } |
| 387 | |
| 388 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) |
| 389 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 390 | struct request_queue *rq; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 391 | |
| 392 | rq = blk_init_queue(do_blkif_request, &blkif_io_lock); |
| 393 | if (rq == NULL) |
| 394 | return -1; |
| 395 | |
Fernando Luis Vázquez Cao | 66d352e | 2008-10-27 18:45:54 +0900 | [diff] [blame] | 396 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 397 | |
| 398 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 399 | blk_queue_logical_block_size(rq, sector_size); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 400 | blk_queue_max_hw_sectors(rq, 512); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 401 | |
| 402 | /* Each segment in a request is up to an aligned page in size. */ |
| 403 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); |
| 404 | blk_queue_max_segment_size(rq, PAGE_SIZE); |
| 405 | |
| 406 | /* Ensure a merged request will fit in a single I/O ring slot. */ |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 407 | blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 408 | |
| 409 | /* Make sure buffer addresses are sector-aligned. */ |
| 410 | blk_queue_dma_alignment(rq, 511); |
| 411 | |
Ian Campbell | 1c91fe1 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 412 | /* Make sure we don't use bounce buffers. */ |
| 413 | blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY); |
| 414 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 415 | gd->queue = rq; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | static int xlvbd_barrier(struct blkfront_info *info) |
| 422 | { |
| 423 | int err; |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 424 | const char *barrier; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 425 | |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 426 | switch (info->feature_barrier) { |
| 427 | case QUEUE_ORDERED_DRAIN: barrier = "enabled (drain)"; break; |
| 428 | case QUEUE_ORDERED_TAG: barrier = "enabled (tag)"; break; |
| 429 | case QUEUE_ORDERED_NONE: barrier = "disabled"; break; |
| 430 | default: return -EINVAL; |
| 431 | } |
Jeremy Fitzhardinge | 4dab46f | 2010-07-22 14:17:00 -0700 | [diff] [blame] | 432 | |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 433 | err = blk_queue_ordered(info->rq, info->feature_barrier); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 434 | |
| 435 | if (err) |
| 436 | return err; |
| 437 | |
| 438 | printk(KERN_INFO "blkfront: %s: barriers %s\n", |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 439 | info->gd->disk_name, barrier); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 444 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
| 445 | struct blkfront_info *info, |
| 446 | u16 vdisk_info, u16 sector_size) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 447 | { |
| 448 | struct gendisk *gd; |
| 449 | int nr_minors = 1; |
| 450 | int err = -ENODEV; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 451 | unsigned int offset; |
| 452 | int minor; |
| 453 | int nr_parts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 454 | |
| 455 | BUG_ON(info->gd != NULL); |
| 456 | BUG_ON(info->rq != NULL); |
| 457 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 458 | if ((info->vdevice>>EXT_SHIFT) > 1) { |
| 459 | /* this is above the extended range; something is wrong */ |
| 460 | printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); |
| 461 | return -ENODEV; |
| 462 | } |
| 463 | |
| 464 | if (!VDEV_IS_EXTENDED(info->vdevice)) { |
| 465 | minor = BLKIF_MINOR(info->vdevice); |
| 466 | nr_parts = PARTS_PER_DISK; |
| 467 | } else { |
| 468 | minor = BLKIF_MINOR_EXT(info->vdevice); |
| 469 | nr_parts = PARTS_PER_EXT_DISK; |
| 470 | } |
| 471 | |
| 472 | if ((minor % nr_parts) == 0) |
| 473 | nr_minors = nr_parts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 474 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 475 | err = xlbd_reserve_minors(minor, nr_minors); |
| 476 | if (err) |
| 477 | goto out; |
| 478 | err = -ENODEV; |
| 479 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 480 | gd = alloc_disk(nr_minors); |
| 481 | if (gd == NULL) |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 482 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 483 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 484 | offset = minor / nr_parts; |
| 485 | |
| 486 | if (nr_minors > 1) { |
| 487 | if (offset < 26) |
| 488 | sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset); |
| 489 | else |
| 490 | sprintf(gd->disk_name, "%s%c%c", DEV_NAME, |
| 491 | 'a' + ((offset / 26)-1), 'a' + (offset % 26)); |
| 492 | } else { |
| 493 | if (offset < 26) |
| 494 | sprintf(gd->disk_name, "%s%c%d", DEV_NAME, |
| 495 | 'a' + offset, |
| 496 | minor & (nr_parts - 1)); |
| 497 | else |
| 498 | sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME, |
| 499 | 'a' + ((offset / 26) - 1), |
| 500 | 'a' + (offset % 26), |
| 501 | minor & (nr_parts - 1)); |
| 502 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 503 | |
| 504 | gd->major = XENVBD_MAJOR; |
| 505 | gd->first_minor = minor; |
| 506 | gd->fops = &xlvbd_block_fops; |
| 507 | gd->private_data = info; |
| 508 | gd->driverfs_dev = &(info->xbdev->dev); |
| 509 | set_capacity(gd, capacity); |
| 510 | |
| 511 | if (xlvbd_init_blk_queue(gd, sector_size)) { |
| 512 | del_gendisk(gd); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 513 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | info->rq = gd->queue; |
| 517 | info->gd = gd; |
| 518 | |
Jeremy Fitzhardinge | 4dab46f | 2010-07-22 14:17:00 -0700 | [diff] [blame] | 519 | xlvbd_barrier(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 520 | |
| 521 | if (vdisk_info & VDISK_READONLY) |
| 522 | set_disk_ro(gd, 1); |
| 523 | |
| 524 | if (vdisk_info & VDISK_REMOVABLE) |
| 525 | gd->flags |= GENHD_FL_REMOVABLE; |
| 526 | |
| 527 | if (vdisk_info & VDISK_CDROM) |
| 528 | gd->flags |= GENHD_FL_CD; |
| 529 | |
| 530 | return 0; |
| 531 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 532 | release: |
| 533 | xlbd_release_minors(minor, nr_minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 534 | out: |
| 535 | return err; |
| 536 | } |
| 537 | |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 538 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
| 539 | { |
| 540 | unsigned int minor, nr_minors; |
| 541 | unsigned long flags; |
| 542 | |
| 543 | if (info->rq == NULL) |
| 544 | return; |
| 545 | |
| 546 | spin_lock_irqsave(&blkif_io_lock, flags); |
| 547 | |
| 548 | /* No more blkif_request(). */ |
| 549 | blk_stop_queue(info->rq); |
| 550 | |
| 551 | /* No more gnttab callback work. */ |
| 552 | gnttab_cancel_free_callback(&info->callback); |
| 553 | spin_unlock_irqrestore(&blkif_io_lock, flags); |
| 554 | |
| 555 | /* Flush gnttab callback work. Must be done with no locks held. */ |
| 556 | flush_scheduled_work(); |
| 557 | |
| 558 | del_gendisk(info->gd); |
| 559 | |
| 560 | minor = info->gd->first_minor; |
| 561 | nr_minors = info->gd->minors; |
| 562 | xlbd_release_minors(minor, nr_minors); |
| 563 | |
| 564 | blk_cleanup_queue(info->rq); |
| 565 | info->rq = NULL; |
| 566 | |
| 567 | put_disk(info->gd); |
| 568 | info->gd = NULL; |
| 569 | } |
| 570 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 571 | static void kick_pending_request_queues(struct blkfront_info *info) |
| 572 | { |
| 573 | if (!RING_FULL(&info->ring)) { |
| 574 | /* Re-enable calldowns. */ |
| 575 | blk_start_queue(info->rq); |
| 576 | /* Kick things off immediately. */ |
| 577 | do_blkif_request(info->rq); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static void blkif_restart_queue(struct work_struct *work) |
| 582 | { |
| 583 | struct blkfront_info *info = container_of(work, struct blkfront_info, work); |
| 584 | |
| 585 | spin_lock_irq(&blkif_io_lock); |
| 586 | if (info->connected == BLKIF_STATE_CONNECTED) |
| 587 | kick_pending_request_queues(info); |
| 588 | spin_unlock_irq(&blkif_io_lock); |
| 589 | } |
| 590 | |
| 591 | static void blkif_free(struct blkfront_info *info, int suspend) |
| 592 | { |
| 593 | /* Prevent new requests being issued until we fix things up. */ |
| 594 | spin_lock_irq(&blkif_io_lock); |
| 595 | info->connected = suspend ? |
| 596 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; |
| 597 | /* No more blkif_request(). */ |
| 598 | if (info->rq) |
| 599 | blk_stop_queue(info->rq); |
| 600 | /* No more gnttab callback work. */ |
| 601 | gnttab_cancel_free_callback(&info->callback); |
| 602 | spin_unlock_irq(&blkif_io_lock); |
| 603 | |
| 604 | /* Flush gnttab callback work. Must be done with no locks held. */ |
| 605 | flush_scheduled_work(); |
| 606 | |
| 607 | /* Free resources associated with old device channel. */ |
| 608 | if (info->ring_ref != GRANT_INVALID_REF) { |
| 609 | gnttab_end_foreign_access(info->ring_ref, 0, |
| 610 | (unsigned long)info->ring.sring); |
| 611 | info->ring_ref = GRANT_INVALID_REF; |
| 612 | info->ring.sring = NULL; |
| 613 | } |
| 614 | if (info->irq) |
| 615 | unbind_from_irqhandler(info->irq, info); |
| 616 | info->evtchn = info->irq = 0; |
| 617 | |
| 618 | } |
| 619 | |
| 620 | static void blkif_completion(struct blk_shadow *s) |
| 621 | { |
| 622 | int i; |
| 623 | for (i = 0; i < s->req.nr_segments; i++) |
| 624 | gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL); |
| 625 | } |
| 626 | |
| 627 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) |
| 628 | { |
| 629 | struct request *req; |
| 630 | struct blkif_response *bret; |
| 631 | RING_IDX i, rp; |
| 632 | unsigned long flags; |
| 633 | struct blkfront_info *info = (struct blkfront_info *)dev_id; |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 634 | int error; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 635 | |
| 636 | spin_lock_irqsave(&blkif_io_lock, flags); |
| 637 | |
| 638 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { |
| 639 | spin_unlock_irqrestore(&blkif_io_lock, flags); |
| 640 | return IRQ_HANDLED; |
| 641 | } |
| 642 | |
| 643 | again: |
| 644 | rp = info->ring.sring->rsp_prod; |
| 645 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
| 646 | |
| 647 | for (i = info->ring.rsp_cons; i != rp; i++) { |
| 648 | unsigned long id; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 649 | |
| 650 | bret = RING_GET_RESPONSE(&info->ring, i); |
| 651 | id = bret->id; |
| 652 | req = (struct request *)info->shadow[id].request; |
| 653 | |
| 654 | blkif_completion(&info->shadow[id]); |
| 655 | |
| 656 | add_id_to_freelist(info, id); |
| 657 | |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 658 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 659 | switch (bret->operation) { |
| 660 | case BLKIF_OP_WRITE_BARRIER: |
| 661 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { |
| 662 | printk(KERN_WARNING "blkfront: %s: write barrier op failed\n", |
| 663 | info->gd->disk_name); |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 664 | error = -EOPNOTSUPP; |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 665 | info->feature_barrier = QUEUE_ORDERED_NONE; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 666 | xlvbd_barrier(info); |
| 667 | } |
| 668 | /* fall through */ |
| 669 | case BLKIF_OP_READ: |
| 670 | case BLKIF_OP_WRITE: |
| 671 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) |
| 672 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " |
| 673 | "request: %x\n", bret->status); |
| 674 | |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 675 | __blk_end_request_all(req, error); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 676 | break; |
| 677 | default: |
| 678 | BUG(); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | info->ring.rsp_cons = i; |
| 683 | |
| 684 | if (i != info->ring.req_prod_pvt) { |
| 685 | int more_to_do; |
| 686 | RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); |
| 687 | if (more_to_do) |
| 688 | goto again; |
| 689 | } else |
| 690 | info->ring.sring->rsp_event = i + 1; |
| 691 | |
| 692 | kick_pending_request_queues(info); |
| 693 | |
| 694 | spin_unlock_irqrestore(&blkif_io_lock, flags); |
| 695 | |
| 696 | return IRQ_HANDLED; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | static int setup_blkring(struct xenbus_device *dev, |
| 701 | struct blkfront_info *info) |
| 702 | { |
| 703 | struct blkif_sring *sring; |
| 704 | int err; |
| 705 | |
| 706 | info->ring_ref = GRANT_INVALID_REF; |
| 707 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 708 | sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 709 | if (!sring) { |
| 710 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); |
| 711 | return -ENOMEM; |
| 712 | } |
| 713 | SHARED_RING_INIT(sring); |
| 714 | FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); |
| 715 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 716 | sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 717 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 718 | err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); |
| 719 | if (err < 0) { |
| 720 | free_page((unsigned long)sring); |
| 721 | info->ring.sring = NULL; |
| 722 | goto fail; |
| 723 | } |
| 724 | info->ring_ref = err; |
| 725 | |
| 726 | err = xenbus_alloc_evtchn(dev, &info->evtchn); |
| 727 | if (err) |
| 728 | goto fail; |
| 729 | |
| 730 | err = bind_evtchn_to_irqhandler(info->evtchn, |
| 731 | blkif_interrupt, |
| 732 | IRQF_SAMPLE_RANDOM, "blkif", info); |
| 733 | if (err <= 0) { |
| 734 | xenbus_dev_fatal(dev, err, |
| 735 | "bind_evtchn_to_irqhandler failed"); |
| 736 | goto fail; |
| 737 | } |
| 738 | info->irq = err; |
| 739 | |
| 740 | return 0; |
| 741 | fail: |
| 742 | blkif_free(info, 0); |
| 743 | return err; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 748 | static int talk_to_blkback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 749 | struct blkfront_info *info) |
| 750 | { |
| 751 | const char *message = NULL; |
| 752 | struct xenbus_transaction xbt; |
| 753 | int err; |
| 754 | |
| 755 | /* Create shared ring, alloc event channel. */ |
| 756 | err = setup_blkring(dev, info); |
| 757 | if (err) |
| 758 | goto out; |
| 759 | |
| 760 | again: |
| 761 | err = xenbus_transaction_start(&xbt); |
| 762 | if (err) { |
| 763 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 764 | goto destroy_blkring; |
| 765 | } |
| 766 | |
| 767 | err = xenbus_printf(xbt, dev->nodename, |
| 768 | "ring-ref", "%u", info->ring_ref); |
| 769 | if (err) { |
| 770 | message = "writing ring-ref"; |
| 771 | goto abort_transaction; |
| 772 | } |
| 773 | err = xenbus_printf(xbt, dev->nodename, |
| 774 | "event-channel", "%u", info->evtchn); |
| 775 | if (err) { |
| 776 | message = "writing event-channel"; |
| 777 | goto abort_transaction; |
| 778 | } |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 779 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
| 780 | XEN_IO_PROTO_ABI_NATIVE); |
| 781 | if (err) { |
| 782 | message = "writing protocol"; |
| 783 | goto abort_transaction; |
| 784 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 785 | |
| 786 | err = xenbus_transaction_end(xbt, 0); |
| 787 | if (err) { |
| 788 | if (err == -EAGAIN) |
| 789 | goto again; |
| 790 | xenbus_dev_fatal(dev, err, "completing transaction"); |
| 791 | goto destroy_blkring; |
| 792 | } |
| 793 | |
| 794 | xenbus_switch_state(dev, XenbusStateInitialised); |
| 795 | |
| 796 | return 0; |
| 797 | |
| 798 | abort_transaction: |
| 799 | xenbus_transaction_end(xbt, 1); |
| 800 | if (message) |
| 801 | xenbus_dev_fatal(dev, err, "%s", message); |
| 802 | destroy_blkring: |
| 803 | blkif_free(info, 0); |
| 804 | out: |
| 805 | return err; |
| 806 | } |
| 807 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 808 | /** |
| 809 | * Entry point to this code when a new device is created. Allocate the basic |
| 810 | * structures and the ring buffer for communication with the backend, and |
| 811 | * inform the backend of the appropriate details for those. Switch to |
| 812 | * Initialised state. |
| 813 | */ |
| 814 | static int blkfront_probe(struct xenbus_device *dev, |
| 815 | const struct xenbus_device_id *id) |
| 816 | { |
| 817 | int err, vdevice, i; |
| 818 | struct blkfront_info *info; |
| 819 | |
| 820 | /* FIXME: Use dynamic device id if this is not set. */ |
| 821 | err = xenbus_scanf(XBT_NIL, dev->nodename, |
| 822 | "virtual-device", "%i", &vdevice); |
| 823 | if (err != 1) { |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 824 | /* go looking in the extended area instead */ |
| 825 | err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", |
| 826 | "%i", &vdevice); |
| 827 | if (err != 1) { |
| 828 | xenbus_dev_fatal(dev, err, "reading virtual-device"); |
| 829 | return err; |
| 830 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 833 | if (xen_hvm_domain()) { |
| 834 | char *type; |
| 835 | int len; |
| 836 | /* no unplug has been done: do not hook devices != xen vbds */ |
Ian Campbell | 1dc7ce9 | 2010-08-23 11:59:29 +0100 | [diff] [blame] | 837 | if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) { |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 838 | int major; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 839 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 840 | if (!VDEV_IS_EXTENDED(vdevice)) |
| 841 | major = BLKIF_MAJOR(vdevice); |
| 842 | else |
| 843 | major = XENVBD_MAJOR; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 844 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 845 | if (major != XENVBD_MAJOR) { |
| 846 | printk(KERN_INFO |
| 847 | "%s: HVM does not support vbd %d as xen block device\n", |
| 848 | __FUNCTION__, vdevice); |
| 849 | return -ENODEV; |
| 850 | } |
| 851 | } |
| 852 | /* do not create a PV cdrom device if we are an HVM guest */ |
| 853 | type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); |
| 854 | if (IS_ERR(type)) |
| 855 | return -ENODEV; |
| 856 | if (strncmp(type, "cdrom", 5) == 0) { |
| 857 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 858 | return -ENODEV; |
| 859 | } |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 860 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 861 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 862 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 863 | if (!info) { |
| 864 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); |
| 865 | return -ENOMEM; |
| 866 | } |
| 867 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 868 | mutex_init(&info->mutex); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 869 | info->xbdev = dev; |
| 870 | info->vdevice = vdevice; |
| 871 | info->connected = BLKIF_STATE_DISCONNECTED; |
| 872 | INIT_WORK(&info->work, blkif_restart_queue); |
| 873 | |
| 874 | for (i = 0; i < BLK_RING_SIZE; i++) |
| 875 | info->shadow[i].req.id = i+1; |
| 876 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; |
| 877 | |
| 878 | /* Front end dir is a number, which is used as the id. */ |
| 879 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 880 | dev_set_drvdata(&dev->dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 881 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 882 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 883 | if (err) { |
| 884 | kfree(info); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 885 | dev_set_drvdata(&dev->dev, NULL); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 886 | return err; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | static int blkif_recover(struct blkfront_info *info) |
| 894 | { |
| 895 | int i; |
| 896 | struct blkif_request *req; |
| 897 | struct blk_shadow *copy; |
| 898 | int j; |
| 899 | |
| 900 | /* Stage 1: Make a safe copy of the shadow state. */ |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 901 | copy = kmalloc(sizeof(info->shadow), |
| 902 | GFP_NOIO | __GFP_REPEAT | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 903 | if (!copy) |
| 904 | return -ENOMEM; |
| 905 | memcpy(copy, info->shadow, sizeof(info->shadow)); |
| 906 | |
| 907 | /* Stage 2: Set up free list. */ |
| 908 | memset(&info->shadow, 0, sizeof(info->shadow)); |
| 909 | for (i = 0; i < BLK_RING_SIZE; i++) |
| 910 | info->shadow[i].req.id = i+1; |
| 911 | info->shadow_free = info->ring.req_prod_pvt; |
| 912 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; |
| 913 | |
| 914 | /* Stage 3: Find pending requests and requeue them. */ |
| 915 | for (i = 0; i < BLK_RING_SIZE; i++) { |
| 916 | /* Not in use? */ |
| 917 | if (copy[i].request == 0) |
| 918 | continue; |
| 919 | |
| 920 | /* Grab a request slot and copy shadow state into it. */ |
| 921 | req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); |
| 922 | *req = copy[i].req; |
| 923 | |
| 924 | /* We get a new request id, and must reset the shadow state. */ |
| 925 | req->id = get_id_from_freelist(info); |
| 926 | memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); |
| 927 | |
| 928 | /* Rewrite any grant references invalidated by susp/resume. */ |
| 929 | for (j = 0; j < req->nr_segments; j++) |
| 930 | gnttab_grant_foreign_access_ref( |
| 931 | req->seg[j].gref, |
| 932 | info->xbdev->otherend_id, |
| 933 | pfn_to_mfn(info->shadow[req->id].frame[j]), |
| 934 | rq_data_dir( |
| 935 | (struct request *) |
| 936 | info->shadow[req->id].request)); |
| 937 | info->shadow[req->id].req = *req; |
| 938 | |
| 939 | info->ring.req_prod_pvt++; |
| 940 | } |
| 941 | |
| 942 | kfree(copy); |
| 943 | |
| 944 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 945 | |
| 946 | spin_lock_irq(&blkif_io_lock); |
| 947 | |
| 948 | /* Now safe for us to use the shared ring */ |
| 949 | info->connected = BLKIF_STATE_CONNECTED; |
| 950 | |
| 951 | /* Send off requeued requests */ |
| 952 | flush_requests(info); |
| 953 | |
| 954 | /* Kick any other new requests queued since we resumed */ |
| 955 | kick_pending_request_queues(info); |
| 956 | |
| 957 | spin_unlock_irq(&blkif_io_lock); |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
| 964 | * driver restart. We tear down our blkif structure and recreate it, but |
| 965 | * leave the device-layer structures intact so that this is transparent to the |
| 966 | * rest of the kernel. |
| 967 | */ |
| 968 | static int blkfront_resume(struct xenbus_device *dev) |
| 969 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 970 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 971 | int err; |
| 972 | |
| 973 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); |
| 974 | |
| 975 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); |
| 976 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 977 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 978 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) |
| 979 | err = blkif_recover(info); |
| 980 | |
| 981 | return err; |
| 982 | } |
| 983 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 984 | static void |
| 985 | blkfront_closing(struct blkfront_info *info) |
| 986 | { |
| 987 | struct xenbus_device *xbdev = info->xbdev; |
| 988 | struct block_device *bdev = NULL; |
| 989 | |
| 990 | mutex_lock(&info->mutex); |
| 991 | |
| 992 | if (xbdev->state == XenbusStateClosing) { |
| 993 | mutex_unlock(&info->mutex); |
| 994 | return; |
| 995 | } |
| 996 | |
| 997 | if (info->gd) |
| 998 | bdev = bdget_disk(info->gd, 0); |
| 999 | |
| 1000 | mutex_unlock(&info->mutex); |
| 1001 | |
| 1002 | if (!bdev) { |
| 1003 | xenbus_frontend_closed(xbdev); |
| 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | mutex_lock(&bdev->bd_mutex); |
| 1008 | |
Daniel Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 1009 | if (bdev->bd_openers) { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1010 | xenbus_dev_error(xbdev, -EBUSY, |
| 1011 | "Device in use; refusing to close"); |
| 1012 | xenbus_switch_state(xbdev, XenbusStateClosing); |
| 1013 | } else { |
| 1014 | xlvbd_release_gendisk(info); |
| 1015 | xenbus_frontend_closed(xbdev); |
| 1016 | } |
| 1017 | |
| 1018 | mutex_unlock(&bdev->bd_mutex); |
| 1019 | bdput(bdev); |
| 1020 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* |
| 1023 | * Invoked when the backend is finally 'ready' (and has told produced |
| 1024 | * the details about the physical device - #sectors, size, etc). |
| 1025 | */ |
| 1026 | static void blkfront_connect(struct blkfront_info *info) |
| 1027 | { |
| 1028 | unsigned long long sectors; |
| 1029 | unsigned long sector_size; |
| 1030 | unsigned int binfo; |
| 1031 | int err; |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1032 | int barrier; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1033 | |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1034 | switch (info->connected) { |
| 1035 | case BLKIF_STATE_CONNECTED: |
| 1036 | /* |
| 1037 | * Potentially, the back-end may be signalling |
| 1038 | * a capacity change; update the capacity. |
| 1039 | */ |
| 1040 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 1041 | "sectors", "%Lu", §ors); |
| 1042 | if (XENBUS_EXIST_ERR(err)) |
| 1043 | return; |
| 1044 | printk(KERN_INFO "Setting capacity to %Lu\n", |
| 1045 | sectors); |
| 1046 | set_capacity(info->gd, sectors); |
K. Y. Srinivasan | 2def141 | 2010-03-18 15:00:54 -0700 | [diff] [blame] | 1047 | revalidate_disk(info->gd); |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1048 | |
| 1049 | /* fall through */ |
| 1050 | case BLKIF_STATE_SUSPENDED: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1051 | return; |
| 1052 | |
Jeremy Fitzhardinge | b4dddb4 | 2010-03-11 15:10:40 -0800 | [diff] [blame] | 1053 | default: |
| 1054 | break; |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1055 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1056 | |
| 1057 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", |
| 1058 | __func__, info->xbdev->otherend); |
| 1059 | |
| 1060 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1061 | "sectors", "%llu", §ors, |
| 1062 | "info", "%u", &binfo, |
| 1063 | "sector-size", "%lu", §or_size, |
| 1064 | NULL); |
| 1065 | if (err) { |
| 1066 | xenbus_dev_fatal(info->xbdev, err, |
| 1067 | "reading backend fields at %s", |
| 1068 | info->xbdev->otherend); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1073 | "feature-barrier", "%lu", &barrier, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1074 | NULL); |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1075 | |
| 1076 | /* |
| 1077 | * If there's no "feature-barrier" defined, then it means |
| 1078 | * we're dealing with a very old backend which writes |
| 1079 | * synchronously; draining will do what needs to get done. |
| 1080 | * |
| 1081 | * If there are barriers, then we can do full queued writes |
| 1082 | * with tagged barriers. |
| 1083 | * |
| 1084 | * If barriers are not supported, then there's no much we can |
| 1085 | * do, so just set ordering to NONE. |
| 1086 | */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1087 | if (err) |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1088 | info->feature_barrier = QUEUE_ORDERED_DRAIN; |
| 1089 | else if (barrier) |
| 1090 | info->feature_barrier = QUEUE_ORDERED_TAG; |
| 1091 | else |
| 1092 | info->feature_barrier = QUEUE_ORDERED_NONE; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1093 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1094 | err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1095 | if (err) { |
| 1096 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", |
| 1097 | info->xbdev->otherend); |
| 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 1102 | |
| 1103 | /* Kick pending requests. */ |
| 1104 | spin_lock_irq(&blkif_io_lock); |
| 1105 | info->connected = BLKIF_STATE_CONNECTED; |
| 1106 | kick_pending_request_queues(info); |
| 1107 | spin_unlock_irq(&blkif_io_lock); |
| 1108 | |
| 1109 | add_disk(info->gd); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1110 | |
| 1111 | info->is_ready = 1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1115 | * Callback received when the backend's state changes. |
| 1116 | */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1117 | static void blkback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1118 | enum xenbus_state backend_state) |
| 1119 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1120 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1121 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1122 | 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] | 1123 | |
| 1124 | switch (backend_state) { |
| 1125 | case XenbusStateInitialising: |
| 1126 | case XenbusStateInitWait: |
| 1127 | case XenbusStateInitialised: |
| 1128 | case XenbusStateUnknown: |
| 1129 | case XenbusStateClosed: |
| 1130 | break; |
| 1131 | |
| 1132 | case XenbusStateConnected: |
| 1133 | blkfront_connect(info); |
| 1134 | break; |
| 1135 | |
| 1136 | case XenbusStateClosing: |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1137 | blkfront_closing(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1142 | static int blkfront_remove(struct xenbus_device *xbdev) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1143 | { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1144 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
| 1145 | struct block_device *bdev = NULL; |
| 1146 | struct gendisk *disk; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1147 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1148 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1149 | |
| 1150 | blkif_free(info, 0); |
| 1151 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1152 | mutex_lock(&info->mutex); |
| 1153 | |
| 1154 | disk = info->gd; |
| 1155 | if (disk) |
| 1156 | bdev = bdget_disk(disk, 0); |
| 1157 | |
| 1158 | info->xbdev = NULL; |
| 1159 | mutex_unlock(&info->mutex); |
| 1160 | |
| 1161 | if (!bdev) { |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1162 | kfree(info); |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * The xbdev was removed before we reached the Closed |
| 1168 | * state. See if it's safe to remove the disk. If the bdev |
| 1169 | * isn't closed yet, we let release take care of it. |
| 1170 | */ |
| 1171 | |
| 1172 | mutex_lock(&bdev->bd_mutex); |
| 1173 | info = disk->private_data; |
| 1174 | |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1175 | dev_warn(disk_to_dev(disk), |
| 1176 | "%s was hot-unplugged, %d stale handles\n", |
| 1177 | xbdev->nodename, bdev->bd_openers); |
| 1178 | |
Daniel Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 1179 | if (info && !bdev->bd_openers) { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1180 | xlvbd_release_gendisk(info); |
| 1181 | disk->private_data = NULL; |
| 1182 | kfree(info); |
| 1183 | } |
| 1184 | |
| 1185 | mutex_unlock(&bdev->bd_mutex); |
| 1186 | bdput(bdev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1191 | static int blkfront_is_ready(struct xenbus_device *dev) |
| 1192 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1193 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1194 | |
Jan Beulich | 5d7ed20 | 2010-08-07 18:31:12 +0200 | [diff] [blame] | 1195 | return info->is_ready && info->xbdev; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1198 | static int blkif_open(struct block_device *bdev, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1199 | { |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1200 | struct gendisk *disk = bdev->bd_disk; |
| 1201 | struct blkfront_info *info; |
| 1202 | int err = 0; |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1203 | |
| 1204 | lock_kernel(); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1205 | |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1206 | info = disk->private_data; |
| 1207 | if (!info) { |
| 1208 | /* xbdev gone */ |
| 1209 | err = -ERESTARTSYS; |
| 1210 | goto out; |
| 1211 | } |
| 1212 | |
| 1213 | mutex_lock(&info->mutex); |
| 1214 | |
| 1215 | if (!info->gd) |
| 1216 | /* xbdev is closed */ |
| 1217 | err = -ERESTARTSYS; |
| 1218 | |
| 1219 | mutex_unlock(&info->mutex); |
| 1220 | |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1221 | out: |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1222 | unlock_kernel(); |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1223 | return err; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1226 | static int blkif_release(struct gendisk *disk, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1227 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1228 | struct blkfront_info *info = disk->private_data; |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1229 | struct block_device *bdev; |
| 1230 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1231 | |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1232 | lock_kernel(); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1233 | |
| 1234 | bdev = bdget_disk(disk, 0); |
| 1235 | bdput(bdev); |
| 1236 | |
Daniel Stodden | acfca3c | 2010-08-07 18:47:26 +0200 | [diff] [blame] | 1237 | if (bdev->bd_openers) |
| 1238 | goto out; |
| 1239 | |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1240 | /* |
| 1241 | * Check if we have been instructed to close. We will have |
| 1242 | * deferred this request, because the bdev was still open. |
| 1243 | */ |
| 1244 | |
| 1245 | mutex_lock(&info->mutex); |
| 1246 | xbdev = info->xbdev; |
| 1247 | |
| 1248 | if (xbdev && xbdev->state == XenbusStateClosing) { |
| 1249 | /* pending switch to state closed */ |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1250 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1251 | xlvbd_release_gendisk(info); |
| 1252 | xenbus_frontend_closed(info->xbdev); |
| 1253 | } |
| 1254 | |
| 1255 | mutex_unlock(&info->mutex); |
| 1256 | |
| 1257 | if (!xbdev) { |
| 1258 | /* sudden device removal */ |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1259 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1260 | xlvbd_release_gendisk(info); |
| 1261 | disk->private_data = NULL; |
| 1262 | kfree(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1263 | } |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1264 | |
Jens Axboe | a4cc14e | 2010-08-08 21:50:05 -0400 | [diff] [blame] | 1265 | out: |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1266 | unlock_kernel(); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1270 | static const struct block_device_operations xlvbd_block_fops = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1271 | { |
| 1272 | .owner = THIS_MODULE, |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1273 | .open = blkif_open, |
| 1274 | .release = blkif_release, |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 1275 | .getgeo = blkif_getgeo, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1276 | .ioctl = blkif_ioctl, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1277 | }; |
| 1278 | |
| 1279 | |
Márton Németh | ec9c42e | 2010-01-10 13:39:52 +0100 | [diff] [blame] | 1280 | static const struct xenbus_device_id blkfront_ids[] = { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1281 | { "vbd" }, |
| 1282 | { "" } |
| 1283 | }; |
| 1284 | |
| 1285 | static struct xenbus_driver blkfront = { |
| 1286 | .name = "vbd", |
| 1287 | .owner = THIS_MODULE, |
| 1288 | .ids = blkfront_ids, |
| 1289 | .probe = blkfront_probe, |
| 1290 | .remove = blkfront_remove, |
| 1291 | .resume = blkfront_resume, |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1292 | .otherend_changed = blkback_changed, |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1293 | .is_ready = blkfront_is_ready, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1294 | }; |
| 1295 | |
| 1296 | static int __init xlblk_init(void) |
| 1297 | { |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 1298 | if (!xen_domain()) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1299 | return -ENODEV; |
| 1300 | |
| 1301 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { |
| 1302 | printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", |
| 1303 | XENVBD_MAJOR, DEV_NAME); |
| 1304 | return -ENODEV; |
| 1305 | } |
| 1306 | |
| 1307 | return xenbus_register_frontend(&blkfront); |
| 1308 | } |
| 1309 | module_init(xlblk_init); |
| 1310 | |
| 1311 | |
Jan Beulich | 5a60d0c | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1312 | static void __exit xlblk_exit(void) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1313 | { |
| 1314 | return xenbus_unregister_driver(&blkfront); |
| 1315 | } |
| 1316 | module_exit(xlblk_exit); |
| 1317 | |
| 1318 | MODULE_DESCRIPTION("Xen virtual block device frontend"); |
| 1319 | MODULE_LICENSE("GPL"); |
| 1320 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 1321 | MODULE_ALIAS("xen:vbd"); |
Mark McLoughlin | 4f93f09b | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 1322 | MODULE_ALIAS("xenblk"); |