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