1 /* Xenbus code for blkif backend 2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au> 3 Copyright (C) 2005 XenSource Ltd 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 */ 16 17 #define pr_fmt(fmt) "xen-blkback: " fmt 18 19 #include <stdarg.h> 20 #include <linux/module.h> 21 #include <linux/kthread.h> 22 #include <xen/events.h> 23 #include <xen/grant_table.h> 24 #include "common.h" 25 26 /* On the XenBus the max length of 'ring-ref%u'. */ 27 #define RINGREF_NAME_LEN (20) 28 29 struct backend_info { 30 struct xenbus_device *dev; 31 struct xen_blkif *blkif; 32 struct xenbus_watch backend_watch; 33 unsigned major; 34 unsigned minor; 35 char *mode; 36 }; 37 38 static struct kmem_cache *xen_blkif_cachep; 39 static void connect(struct backend_info *); 40 static int connect_ring(struct backend_info *); 41 static void backend_changed(struct xenbus_watch *, const char **, 42 unsigned int); 43 static void xen_blkif_free(struct xen_blkif *blkif); 44 static void xen_vbd_free(struct xen_vbd *vbd); 45 46 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) 47 { 48 return be->dev; 49 } 50 51 /* 52 * The last request could free the device from softirq context and 53 * xen_blkif_free() can sleep. 54 */ 55 static void xen_blkif_deferred_free(struct work_struct *work) 56 { 57 struct xen_blkif *blkif; 58 59 blkif = container_of(work, struct xen_blkif, free_work); 60 xen_blkif_free(blkif); 61 } 62 63 static int blkback_name(struct xen_blkif *blkif, char *buf) 64 { 65 char *devpath, *devname; 66 struct xenbus_device *dev = blkif->be->dev; 67 68 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); 69 if (IS_ERR(devpath)) 70 return PTR_ERR(devpath); 71 72 devname = strstr(devpath, "/dev/"); 73 if (devname != NULL) 74 devname += strlen("/dev/"); 75 else 76 devname = devpath; 77 78 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname); 79 kfree(devpath); 80 81 return 0; 82 } 83 84 static void xen_update_blkif_status(struct xen_blkif *blkif) 85 { 86 int err; 87 char name[TASK_COMM_LEN]; 88 struct xen_blkif_ring *ring; 89 int i; 90 91 /* Not ready to connect? */ 92 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev) 93 return; 94 95 /* Already connected? */ 96 if (blkif->be->dev->state == XenbusStateConnected) 97 return; 98 99 /* Attempt to connect: exit if we fail to. */ 100 connect(blkif->be); 101 if (blkif->be->dev->state != XenbusStateConnected) 102 return; 103 104 err = blkback_name(blkif, name); 105 if (err) { 106 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); 107 return; 108 } 109 110 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); 111 if (err) { 112 xenbus_dev_error(blkif->be->dev, err, "block flush"); 113 return; 114 } 115 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); 116 117 for (i = 0; i < blkif->nr_rings; i++) { 118 ring = &blkif->rings[i]; 119 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i); 120 if (IS_ERR(ring->xenblkd)) { 121 err = PTR_ERR(ring->xenblkd); 122 ring->xenblkd = NULL; 123 xenbus_dev_fatal(blkif->be->dev, err, 124 "start %s-%d xenblkd", name, i); 125 goto out; 126 } 127 } 128 return; 129 130 out: 131 while (--i >= 0) { 132 ring = &blkif->rings[i]; 133 kthread_stop(ring->xenblkd); 134 } 135 return; 136 } 137 138 static int xen_blkif_alloc_rings(struct xen_blkif *blkif) 139 { 140 unsigned int r; 141 142 blkif->rings = kzalloc(blkif->nr_rings * sizeof(struct xen_blkif_ring), GFP_KERNEL); 143 if (!blkif->rings) 144 return -ENOMEM; 145 146 for (r = 0; r < blkif->nr_rings; r++) { 147 struct xen_blkif_ring *ring = &blkif->rings[r]; 148 149 spin_lock_init(&ring->blk_ring_lock); 150 init_waitqueue_head(&ring->wq); 151 INIT_LIST_HEAD(&ring->pending_free); 152 INIT_LIST_HEAD(&ring->persistent_purge_list); 153 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants); 154 spin_lock_init(&ring->free_pages_lock); 155 INIT_LIST_HEAD(&ring->free_pages); 156 157 spin_lock_init(&ring->pending_free_lock); 158 init_waitqueue_head(&ring->pending_free_wq); 159 init_waitqueue_head(&ring->shutdown_wq); 160 ring->blkif = blkif; 161 ring->st_print = jiffies; 162 xen_blkif_get(blkif); 163 } 164 165 return 0; 166 } 167 168 static struct xen_blkif *xen_blkif_alloc(domid_t domid) 169 { 170 struct xen_blkif *blkif; 171 172 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST); 173 174 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL); 175 if (!blkif) 176 return ERR_PTR(-ENOMEM); 177 178 blkif->domid = domid; 179 atomic_set(&blkif->refcnt, 1); 180 init_completion(&blkif->drain_complete); 181 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); 182 183 return blkif; 184 } 185 186 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref, 187 unsigned int nr_grefs, unsigned int evtchn) 188 { 189 int err; 190 struct xen_blkif *blkif = ring->blkif; 191 192 /* Already connected through? */ 193 if (ring->irq) 194 return 0; 195 196 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs, 197 &ring->blk_ring); 198 if (err < 0) 199 return err; 200 201 switch (blkif->blk_protocol) { 202 case BLKIF_PROTOCOL_NATIVE: 203 { 204 struct blkif_sring *sring; 205 sring = (struct blkif_sring *)ring->blk_ring; 206 BACK_RING_INIT(&ring->blk_rings.native, sring, 207 XEN_PAGE_SIZE * nr_grefs); 208 break; 209 } 210 case BLKIF_PROTOCOL_X86_32: 211 { 212 struct blkif_x86_32_sring *sring_x86_32; 213 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring; 214 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32, 215 XEN_PAGE_SIZE * nr_grefs); 216 break; 217 } 218 case BLKIF_PROTOCOL_X86_64: 219 { 220 struct blkif_x86_64_sring *sring_x86_64; 221 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring; 222 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64, 223 XEN_PAGE_SIZE * nr_grefs); 224 break; 225 } 226 default: 227 BUG(); 228 } 229 230 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, 231 xen_blkif_be_int, 0, 232 "blkif-backend", ring); 233 if (err < 0) { 234 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring); 235 ring->blk_rings.common.sring = NULL; 236 return err; 237 } 238 ring->irq = err; 239 240 return 0; 241 } 242 243 static int xen_blkif_disconnect(struct xen_blkif *blkif) 244 { 245 struct pending_req *req, *n; 246 unsigned int j, r; 247 248 for (r = 0; r < blkif->nr_rings; r++) { 249 struct xen_blkif_ring *ring = &blkif->rings[r]; 250 unsigned int i = 0; 251 252 if (ring->xenblkd) { 253 kthread_stop(ring->xenblkd); 254 wake_up(&ring->shutdown_wq); 255 ring->xenblkd = NULL; 256 } 257 258 /* The above kthread_stop() guarantees that at this point we 259 * don't have any discard_io or other_io requests. So, checking 260 * for inflight IO is enough. 261 */ 262 if (atomic_read(&ring->inflight) > 0) 263 return -EBUSY; 264 265 if (ring->irq) { 266 unbind_from_irqhandler(ring->irq, ring); 267 ring->irq = 0; 268 } 269 270 if (ring->blk_rings.common.sring) { 271 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring); 272 ring->blk_rings.common.sring = NULL; 273 } 274 275 /* Remove all persistent grants and the cache of ballooned pages. */ 276 xen_blkbk_free_caches(ring); 277 278 /* Check that there is no request in use */ 279 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) { 280 list_del(&req->free_list); 281 282 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) 283 kfree(req->segments[j]); 284 285 for (j = 0; j < MAX_INDIRECT_PAGES; j++) 286 kfree(req->indirect_pages[j]); 287 288 kfree(req); 289 i++; 290 } 291 292 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0); 293 BUG_ON(!list_empty(&ring->persistent_purge_list)); 294 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts)); 295 BUG_ON(!list_empty(&ring->free_pages)); 296 BUG_ON(ring->free_pages_num != 0); 297 BUG_ON(ring->persistent_gnt_c != 0); 298 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages)); 299 xen_blkif_put(blkif); 300 } 301 blkif->nr_ring_pages = 0; 302 /* 303 * blkif->rings was allocated in connect_ring, so we should free it in 304 * here. 305 */ 306 kfree(blkif->rings); 307 blkif->rings = NULL; 308 blkif->nr_rings = 0; 309 310 return 0; 311 } 312 313 static void xen_blkif_free(struct xen_blkif *blkif) 314 { 315 316 xen_blkif_disconnect(blkif); 317 xen_vbd_free(&blkif->vbd); 318 319 /* Make sure everything is drained before shutting down */ 320 kmem_cache_free(xen_blkif_cachep, blkif); 321 } 322 323 int __init xen_blkif_interface_init(void) 324 { 325 xen_blkif_cachep = kmem_cache_create("blkif_cache", 326 sizeof(struct xen_blkif), 327 0, 0, NULL); 328 if (!xen_blkif_cachep) 329 return -ENOMEM; 330 331 return 0; 332 } 333 334 /* 335 * sysfs interface for VBD I/O requests 336 */ 337 338 #define VBD_SHOW_ALLRING(name, format) \ 339 static ssize_t show_##name(struct device *_dev, \ 340 struct device_attribute *attr, \ 341 char *buf) \ 342 { \ 343 struct xenbus_device *dev = to_xenbus_device(_dev); \ 344 struct backend_info *be = dev_get_drvdata(&dev->dev); \ 345 struct xen_blkif *blkif = be->blkif; \ 346 unsigned int i; \ 347 unsigned long long result = 0; \ 348 \ 349 if (!blkif->rings) \ 350 goto out; \ 351 \ 352 for (i = 0; i < blkif->nr_rings; i++) { \ 353 struct xen_blkif_ring *ring = &blkif->rings[i]; \ 354 \ 355 result += ring->st_##name; \ 356 } \ 357 \ 358 out: \ 359 return sprintf(buf, format, result); \ 360 } \ 361 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) 362 363 VBD_SHOW_ALLRING(oo_req, "%llu\n"); 364 VBD_SHOW_ALLRING(rd_req, "%llu\n"); 365 VBD_SHOW_ALLRING(wr_req, "%llu\n"); 366 VBD_SHOW_ALLRING(f_req, "%llu\n"); 367 VBD_SHOW_ALLRING(ds_req, "%llu\n"); 368 VBD_SHOW_ALLRING(rd_sect, "%llu\n"); 369 VBD_SHOW_ALLRING(wr_sect, "%llu\n"); 370 371 static struct attribute *xen_vbdstat_attrs[] = { 372 &dev_attr_oo_req.attr, 373 &dev_attr_rd_req.attr, 374 &dev_attr_wr_req.attr, 375 &dev_attr_f_req.attr, 376 &dev_attr_ds_req.attr, 377 &dev_attr_rd_sect.attr, 378 &dev_attr_wr_sect.attr, 379 NULL 380 }; 381 382 static struct attribute_group xen_vbdstat_group = { 383 .name = "statistics", 384 .attrs = xen_vbdstat_attrs, 385 }; 386 387 #define VBD_SHOW(name, format, args...) \ 388 static ssize_t show_##name(struct device *_dev, \ 389 struct device_attribute *attr, \ 390 char *buf) \ 391 { \ 392 struct xenbus_device *dev = to_xenbus_device(_dev); \ 393 struct backend_info *be = dev_get_drvdata(&dev->dev); \ 394 \ 395 return sprintf(buf, format, ##args); \ 396 } \ 397 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) 398 399 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); 400 VBD_SHOW(mode, "%s\n", be->mode); 401 402 static int xenvbd_sysfs_addif(struct xenbus_device *dev) 403 { 404 int error; 405 406 error = device_create_file(&dev->dev, &dev_attr_physical_device); 407 if (error) 408 goto fail1; 409 410 error = device_create_file(&dev->dev, &dev_attr_mode); 411 if (error) 412 goto fail2; 413 414 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); 415 if (error) 416 goto fail3; 417 418 return 0; 419 420 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 421 fail2: device_remove_file(&dev->dev, &dev_attr_mode); 422 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); 423 return error; 424 } 425 426 static void xenvbd_sysfs_delif(struct xenbus_device *dev) 427 { 428 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 429 device_remove_file(&dev->dev, &dev_attr_mode); 430 device_remove_file(&dev->dev, &dev_attr_physical_device); 431 } 432 433 434 static void xen_vbd_free(struct xen_vbd *vbd) 435 { 436 if (vbd->bdev) 437 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); 438 vbd->bdev = NULL; 439 } 440 441 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, 442 unsigned major, unsigned minor, int readonly, 443 int cdrom) 444 { 445 struct xen_vbd *vbd; 446 struct block_device *bdev; 447 struct request_queue *q; 448 449 vbd = &blkif->vbd; 450 vbd->handle = handle; 451 vbd->readonly = readonly; 452 vbd->type = 0; 453 454 vbd->pdevice = MKDEV(major, minor); 455 456 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? 457 FMODE_READ : FMODE_WRITE, NULL); 458 459 if (IS_ERR(bdev)) { 460 pr_warn("xen_vbd_create: device %08x could not be opened\n", 461 vbd->pdevice); 462 return -ENOENT; 463 } 464 465 vbd->bdev = bdev; 466 if (vbd->bdev->bd_disk == NULL) { 467 pr_warn("xen_vbd_create: device %08x doesn't exist\n", 468 vbd->pdevice); 469 xen_vbd_free(vbd); 470 return -ENOENT; 471 } 472 vbd->size = vbd_sz(vbd); 473 474 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) 475 vbd->type |= VDISK_CDROM; 476 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) 477 vbd->type |= VDISK_REMOVABLE; 478 479 q = bdev_get_queue(bdev); 480 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 481 vbd->flush_support = true; 482 483 if (q && blk_queue_secdiscard(q)) 484 vbd->discard_secure = true; 485 486 pr_debug("Successful creation of handle=%04x (dom=%u)\n", 487 handle, blkif->domid); 488 return 0; 489 } 490 static int xen_blkbk_remove(struct xenbus_device *dev) 491 { 492 struct backend_info *be = dev_get_drvdata(&dev->dev); 493 494 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 495 496 if (be->major || be->minor) 497 xenvbd_sysfs_delif(dev); 498 499 if (be->backend_watch.node) { 500 unregister_xenbus_watch(&be->backend_watch); 501 kfree(be->backend_watch.node); 502 be->backend_watch.node = NULL; 503 } 504 505 dev_set_drvdata(&dev->dev, NULL); 506 507 if (be->blkif) 508 xen_blkif_disconnect(be->blkif); 509 510 /* Put the reference we set in xen_blkif_alloc(). */ 511 xen_blkif_put(be->blkif); 512 kfree(be->mode); 513 kfree(be); 514 return 0; 515 } 516 517 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, 518 struct backend_info *be, int state) 519 { 520 struct xenbus_device *dev = be->dev; 521 int err; 522 523 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", 524 "%d", state); 525 if (err) 526 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err); 527 528 return err; 529 } 530 531 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) 532 { 533 struct xenbus_device *dev = be->dev; 534 struct xen_blkif *blkif = be->blkif; 535 int err; 536 int state = 0, discard_enable; 537 struct block_device *bdev = be->blkif->vbd.bdev; 538 struct request_queue *q = bdev_get_queue(bdev); 539 540 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d", 541 &discard_enable); 542 if (err == 1 && !discard_enable) 543 return; 544 545 if (blk_queue_discard(q)) { 546 err = xenbus_printf(xbt, dev->nodename, 547 "discard-granularity", "%u", 548 q->limits.discard_granularity); 549 if (err) { 550 dev_warn(&dev->dev, "writing discard-granularity (%d)", err); 551 return; 552 } 553 err = xenbus_printf(xbt, dev->nodename, 554 "discard-alignment", "%u", 555 q->limits.discard_alignment); 556 if (err) { 557 dev_warn(&dev->dev, "writing discard-alignment (%d)", err); 558 return; 559 } 560 state = 1; 561 /* Optional. */ 562 err = xenbus_printf(xbt, dev->nodename, 563 "discard-secure", "%d", 564 blkif->vbd.discard_secure); 565 if (err) { 566 dev_warn(&dev->dev, "writing discard-secure (%d)", err); 567 return; 568 } 569 } 570 err = xenbus_printf(xbt, dev->nodename, "feature-discard", 571 "%d", state); 572 if (err) 573 dev_warn(&dev->dev, "writing feature-discard (%d)", err); 574 } 575 int xen_blkbk_barrier(struct xenbus_transaction xbt, 576 struct backend_info *be, int state) 577 { 578 struct xenbus_device *dev = be->dev; 579 int err; 580 581 err = xenbus_printf(xbt, dev->nodename, "feature-barrier", 582 "%d", state); 583 if (err) 584 dev_warn(&dev->dev, "writing feature-barrier (%d)", err); 585 586 return err; 587 } 588 589 /* 590 * Entry point to this code when a new device is created. Allocate the basic 591 * structures, and watch the store waiting for the hotplug scripts to tell us 592 * the device's physical major and minor numbers. Switch to InitWait. 593 */ 594 static int xen_blkbk_probe(struct xenbus_device *dev, 595 const struct xenbus_device_id *id) 596 { 597 int err; 598 struct backend_info *be = kzalloc(sizeof(struct backend_info), 599 GFP_KERNEL); 600 601 /* match the pr_debug in xen_blkbk_remove */ 602 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 603 604 if (!be) { 605 xenbus_dev_fatal(dev, -ENOMEM, 606 "allocating backend structure"); 607 return -ENOMEM; 608 } 609 be->dev = dev; 610 dev_set_drvdata(&dev->dev, be); 611 612 be->blkif = xen_blkif_alloc(dev->otherend_id); 613 if (IS_ERR(be->blkif)) { 614 err = PTR_ERR(be->blkif); 615 be->blkif = NULL; 616 xenbus_dev_fatal(dev, err, "creating block interface"); 617 goto fail; 618 } 619 620 err = xenbus_printf(XBT_NIL, dev->nodename, 621 "feature-max-indirect-segments", "%u", 622 MAX_INDIRECT_SEGMENTS); 623 if (err) 624 dev_warn(&dev->dev, 625 "writing %s/feature-max-indirect-segments (%d)", 626 dev->nodename, err); 627 628 /* Multi-queue: advertise how many queues are supported by us.*/ 629 err = xenbus_printf(XBT_NIL, dev->nodename, 630 "multi-queue-max-queues", "%u", xenblk_max_queues); 631 if (err) 632 pr_warn("Error writing multi-queue-max-queues\n"); 633 634 /* setup back pointer */ 635 be->blkif->be = be; 636 637 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, 638 "%s/%s", dev->nodename, "physical-device"); 639 if (err) 640 goto fail; 641 642 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u", 643 xen_blkif_max_ring_order); 644 if (err) 645 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__); 646 647 err = xenbus_switch_state(dev, XenbusStateInitWait); 648 if (err) 649 goto fail; 650 651 return 0; 652 653 fail: 654 pr_warn("%s failed\n", __func__); 655 xen_blkbk_remove(dev); 656 return err; 657 } 658 659 660 /* 661 * Callback received when the hotplug scripts have placed the physical-device 662 * node. Read it and the mode node, and create a vbd. If the frontend is 663 * ready, connect. 664 */ 665 static void backend_changed(struct xenbus_watch *watch, 666 const char **vec, unsigned int len) 667 { 668 int err; 669 unsigned major; 670 unsigned minor; 671 struct backend_info *be 672 = container_of(watch, struct backend_info, backend_watch); 673 struct xenbus_device *dev = be->dev; 674 int cdrom = 0; 675 unsigned long handle; 676 char *device_type; 677 678 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 679 680 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", 681 &major, &minor); 682 if (XENBUS_EXIST_ERR(err)) { 683 /* 684 * Since this watch will fire once immediately after it is 685 * registered, we expect this. Ignore it, and wait for the 686 * hotplug scripts. 687 */ 688 return; 689 } 690 if (err != 2) { 691 xenbus_dev_fatal(dev, err, "reading physical-device"); 692 return; 693 } 694 695 if (be->major | be->minor) { 696 if (be->major != major || be->minor != minor) 697 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n", 698 be->major, be->minor, major, minor); 699 return; 700 } 701 702 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); 703 if (IS_ERR(be->mode)) { 704 err = PTR_ERR(be->mode); 705 be->mode = NULL; 706 xenbus_dev_fatal(dev, err, "reading mode"); 707 return; 708 } 709 710 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); 711 if (!IS_ERR(device_type)) { 712 cdrom = strcmp(device_type, "cdrom") == 0; 713 kfree(device_type); 714 } 715 716 /* Front end dir is a number, which is used as the handle. */ 717 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle); 718 if (err) 719 return; 720 721 be->major = major; 722 be->minor = minor; 723 724 err = xen_vbd_create(be->blkif, handle, major, minor, 725 !strchr(be->mode, 'w'), cdrom); 726 727 if (err) 728 xenbus_dev_fatal(dev, err, "creating vbd structure"); 729 else { 730 err = xenvbd_sysfs_addif(dev); 731 if (err) { 732 xen_vbd_free(&be->blkif->vbd); 733 xenbus_dev_fatal(dev, err, "creating sysfs entries"); 734 } 735 } 736 737 if (err) { 738 kfree(be->mode); 739 be->mode = NULL; 740 be->major = 0; 741 be->minor = 0; 742 } else { 743 /* We're potentially connected now */ 744 xen_update_blkif_status(be->blkif); 745 } 746 } 747 748 749 /* 750 * Callback received when the frontend's state changes. 751 */ 752 static void frontend_changed(struct xenbus_device *dev, 753 enum xenbus_state frontend_state) 754 { 755 struct backend_info *be = dev_get_drvdata(&dev->dev); 756 int err; 757 758 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state)); 759 760 switch (frontend_state) { 761 case XenbusStateInitialising: 762 if (dev->state == XenbusStateClosed) { 763 pr_info("%s: prepare for reconnect\n", dev->nodename); 764 xenbus_switch_state(dev, XenbusStateInitWait); 765 } 766 break; 767 768 case XenbusStateInitialised: 769 case XenbusStateConnected: 770 /* 771 * Ensure we connect even when two watches fire in 772 * close succession and we miss the intermediate value 773 * of frontend_state. 774 */ 775 if (dev->state == XenbusStateConnected) 776 break; 777 778 /* 779 * Enforce precondition before potential leak point. 780 * xen_blkif_disconnect() is idempotent. 781 */ 782 err = xen_blkif_disconnect(be->blkif); 783 if (err) { 784 xenbus_dev_fatal(dev, err, "pending I/O"); 785 break; 786 } 787 788 err = connect_ring(be); 789 if (err) { 790 /* 791 * Clean up so that memory resources can be used by 792 * other devices. connect_ring reported already error. 793 */ 794 xen_blkif_disconnect(be->blkif); 795 break; 796 } 797 xen_update_blkif_status(be->blkif); 798 break; 799 800 case XenbusStateClosing: 801 xenbus_switch_state(dev, XenbusStateClosing); 802 break; 803 804 case XenbusStateClosed: 805 xen_blkif_disconnect(be->blkif); 806 xenbus_switch_state(dev, XenbusStateClosed); 807 if (xenbus_dev_is_online(dev)) 808 break; 809 /* fall through if not online */ 810 case XenbusStateUnknown: 811 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ 812 device_unregister(&dev->dev); 813 break; 814 815 default: 816 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", 817 frontend_state); 818 break; 819 } 820 } 821 822 823 /* ** Connection ** */ 824 825 826 /* 827 * Write the physical details regarding the block device to the store, and 828 * switch to Connected state. 829 */ 830 static void connect(struct backend_info *be) 831 { 832 struct xenbus_transaction xbt; 833 int err; 834 struct xenbus_device *dev = be->dev; 835 836 pr_debug("%s %s\n", __func__, dev->otherend); 837 838 /* Supply the information about the device the frontend needs */ 839 again: 840 err = xenbus_transaction_start(&xbt); 841 if (err) { 842 xenbus_dev_fatal(dev, err, "starting transaction"); 843 return; 844 } 845 846 /* If we can't advertise it is OK. */ 847 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); 848 849 xen_blkbk_discard(xbt, be); 850 851 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); 852 853 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1); 854 if (err) { 855 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", 856 dev->nodename); 857 goto abort; 858 } 859 860 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", 861 (unsigned long long)vbd_sz(&be->blkif->vbd)); 862 if (err) { 863 xenbus_dev_fatal(dev, err, "writing %s/sectors", 864 dev->nodename); 865 goto abort; 866 } 867 868 /* FIXME: use a typename instead */ 869 err = xenbus_printf(xbt, dev->nodename, "info", "%u", 870 be->blkif->vbd.type | 871 (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); 872 if (err) { 873 xenbus_dev_fatal(dev, err, "writing %s/info", 874 dev->nodename); 875 goto abort; 876 } 877 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", 878 (unsigned long) 879 bdev_logical_block_size(be->blkif->vbd.bdev)); 880 if (err) { 881 xenbus_dev_fatal(dev, err, "writing %s/sector-size", 882 dev->nodename); 883 goto abort; 884 } 885 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u", 886 bdev_physical_block_size(be->blkif->vbd.bdev)); 887 if (err) 888 xenbus_dev_error(dev, err, "writing %s/physical-sector-size", 889 dev->nodename); 890 891 err = xenbus_transaction_end(xbt, 0); 892 if (err == -EAGAIN) 893 goto again; 894 if (err) 895 xenbus_dev_fatal(dev, err, "ending transaction"); 896 897 err = xenbus_switch_state(dev, XenbusStateConnected); 898 if (err) 899 xenbus_dev_fatal(dev, err, "%s: switching to Connected state", 900 dev->nodename); 901 902 return; 903 abort: 904 xenbus_transaction_end(xbt, 1); 905 } 906 907 /* 908 * Each ring may have multi pages, depends on "ring-page-order". 909 */ 910 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir) 911 { 912 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; 913 struct pending_req *req, *n; 914 int err, i, j; 915 struct xen_blkif *blkif = ring->blkif; 916 struct xenbus_device *dev = blkif->be->dev; 917 unsigned int ring_page_order, nr_grefs, evtchn; 918 919 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u", 920 &evtchn); 921 if (err != 1) { 922 err = -EINVAL; 923 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir); 924 return err; 925 } 926 927 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u", 928 &ring_page_order); 929 if (err != 1) { 930 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]); 931 if (err != 1) { 932 err = -EINVAL; 933 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir); 934 return err; 935 } 936 nr_grefs = 1; 937 } else { 938 unsigned int i; 939 940 if (ring_page_order > xen_blkif_max_ring_order) { 941 err = -EINVAL; 942 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d", 943 dir, ring_page_order, 944 xen_blkif_max_ring_order); 945 return err; 946 } 947 948 nr_grefs = 1 << ring_page_order; 949 for (i = 0; i < nr_grefs; i++) { 950 char ring_ref_name[RINGREF_NAME_LEN]; 951 952 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); 953 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name, 954 "%u", &ring_ref[i]); 955 if (err != 1) { 956 err = -EINVAL; 957 xenbus_dev_fatal(dev, err, "reading %s/%s", 958 dir, ring_ref_name); 959 return err; 960 } 961 } 962 } 963 blkif->nr_ring_pages = nr_grefs; 964 965 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) { 966 req = kzalloc(sizeof(*req), GFP_KERNEL); 967 if (!req) 968 goto fail; 969 list_add_tail(&req->free_list, &ring->pending_free); 970 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 971 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL); 972 if (!req->segments[j]) 973 goto fail; 974 } 975 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 976 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]), 977 GFP_KERNEL); 978 if (!req->indirect_pages[j]) 979 goto fail; 980 } 981 } 982 983 /* Map the shared frame, irq etc. */ 984 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn); 985 if (err) { 986 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn); 987 return err; 988 } 989 990 return 0; 991 992 fail: 993 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) { 994 list_del(&req->free_list); 995 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 996 if (!req->segments[j]) 997 break; 998 kfree(req->segments[j]); 999 } 1000 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 1001 if (!req->indirect_pages[j]) 1002 break; 1003 kfree(req->indirect_pages[j]); 1004 } 1005 kfree(req); 1006 } 1007 return -ENOMEM; 1008 1009 } 1010 1011 static int connect_ring(struct backend_info *be) 1012 { 1013 struct xenbus_device *dev = be->dev; 1014 unsigned int pers_grants; 1015 char protocol[64] = ""; 1016 int err, i; 1017 char *xspath; 1018 size_t xspathsize; 1019 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */ 1020 unsigned int requested_num_queues = 0; 1021 1022 pr_debug("%s %s\n", __func__, dev->otherend); 1023 1024 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT; 1025 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", 1026 "%63s", protocol, NULL); 1027 if (err) 1028 strcpy(protocol, "unspecified, assuming default"); 1029 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) 1030 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; 1031 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) 1032 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; 1033 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) 1034 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; 1035 else { 1036 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); 1037 return -ENOSYS; 1038 } 1039 err = xenbus_gather(XBT_NIL, dev->otherend, 1040 "feature-persistent", "%u", 1041 &pers_grants, NULL); 1042 if (err) 1043 pers_grants = 0; 1044 1045 be->blkif->vbd.feature_gnt_persistent = pers_grants; 1046 be->blkif->vbd.overflow_max_grants = 0; 1047 1048 /* 1049 * Read the number of hardware queues from frontend. 1050 */ 1051 err = xenbus_scanf(XBT_NIL, dev->otherend, "multi-queue-num-queues", 1052 "%u", &requested_num_queues); 1053 if (err < 0) { 1054 requested_num_queues = 1; 1055 } else { 1056 if (requested_num_queues > xenblk_max_queues 1057 || requested_num_queues == 0) { 1058 /* Buggy or malicious guest. */ 1059 xenbus_dev_fatal(dev, err, 1060 "guest requested %u queues, exceeding the maximum of %u.", 1061 requested_num_queues, xenblk_max_queues); 1062 return -ENOSYS; 1063 } 1064 } 1065 be->blkif->nr_rings = requested_num_queues; 1066 if (xen_blkif_alloc_rings(be->blkif)) 1067 return -ENOMEM; 1068 1069 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename, 1070 be->blkif->nr_rings, be->blkif->blk_protocol, protocol, 1071 pers_grants ? "persistent grants" : ""); 1072 1073 if (be->blkif->nr_rings == 1) 1074 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend); 1075 else { 1076 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size; 1077 xspath = kmalloc(xspathsize, GFP_KERNEL); 1078 if (!xspath) { 1079 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references"); 1080 return -ENOMEM; 1081 } 1082 1083 for (i = 0; i < be->blkif->nr_rings; i++) { 1084 memset(xspath, 0, xspathsize); 1085 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i); 1086 err = read_per_ring_refs(&be->blkif->rings[i], xspath); 1087 if (err) { 1088 kfree(xspath); 1089 return err; 1090 } 1091 } 1092 kfree(xspath); 1093 } 1094 return 0; 1095 } 1096 1097 static const struct xenbus_device_id xen_blkbk_ids[] = { 1098 { "vbd" }, 1099 { "" } 1100 }; 1101 1102 static struct xenbus_driver xen_blkbk_driver = { 1103 .ids = xen_blkbk_ids, 1104 .probe = xen_blkbk_probe, 1105 .remove = xen_blkbk_remove, 1106 .otherend_changed = frontend_changed 1107 }; 1108 1109 int xen_blkif_xenbus_init(void) 1110 { 1111 return xenbus_register_backend(&xen_blkbk_driver); 1112 } 1113