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