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