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