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