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 175 /* 176 * Because freeing back to the cache may be deferred, it is not 177 * safe to unload the module (and hence destroy the cache) until 178 * this has completed. To prevent premature unloading, take an 179 * extra module reference here and release only when the object 180 * has been freed back to the cache. 181 */ 182 __module_get(THIS_MODULE); 183 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); 184 185 return blkif; 186 } 187 188 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref, 189 unsigned int nr_grefs, unsigned int evtchn) 190 { 191 int err; 192 struct xen_blkif *blkif = ring->blkif; 193 const struct blkif_common_sring *sring_common; 194 RING_IDX rsp_prod, req_prod; 195 unsigned int size; 196 197 /* Already connected through? */ 198 if (ring->irq) 199 return 0; 200 201 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs, 202 &ring->blk_ring); 203 if (err < 0) 204 return err; 205 206 sring_common = (struct blkif_common_sring *)ring->blk_ring; 207 rsp_prod = READ_ONCE(sring_common->rsp_prod); 208 req_prod = READ_ONCE(sring_common->req_prod); 209 210 switch (blkif->blk_protocol) { 211 case BLKIF_PROTOCOL_NATIVE: 212 { 213 struct blkif_sring *sring_native = 214 (struct blkif_sring *)ring->blk_ring; 215 216 BACK_RING_ATTACH(&ring->blk_rings.native, sring_native, 217 rsp_prod, XEN_PAGE_SIZE * nr_grefs); 218 size = __RING_SIZE(sring_native, XEN_PAGE_SIZE * nr_grefs); 219 break; 220 } 221 case BLKIF_PROTOCOL_X86_32: 222 { 223 struct blkif_x86_32_sring *sring_x86_32 = 224 (struct blkif_x86_32_sring *)ring->blk_ring; 225 226 BACK_RING_ATTACH(&ring->blk_rings.x86_32, sring_x86_32, 227 rsp_prod, XEN_PAGE_SIZE * nr_grefs); 228 size = __RING_SIZE(sring_x86_32, XEN_PAGE_SIZE * nr_grefs); 229 break; 230 } 231 case BLKIF_PROTOCOL_X86_64: 232 { 233 struct blkif_x86_64_sring *sring_x86_64 = 234 (struct blkif_x86_64_sring *)ring->blk_ring; 235 236 BACK_RING_ATTACH(&ring->blk_rings.x86_64, sring_x86_64, 237 rsp_prod, XEN_PAGE_SIZE * nr_grefs); 238 size = __RING_SIZE(sring_x86_64, XEN_PAGE_SIZE * nr_grefs); 239 break; 240 } 241 default: 242 BUG(); 243 } 244 245 err = -EIO; 246 if (req_prod - rsp_prod > size) 247 goto fail; 248 249 err = bind_interdomain_evtchn_to_irqhandler_lateeoi(blkif->domid, 250 evtchn, xen_blkif_be_int, 0, "blkif-backend", ring); 251 if (err < 0) 252 goto fail; 253 ring->irq = err; 254 255 return 0; 256 257 fail: 258 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring); 259 ring->blk_rings.common.sring = NULL; 260 return err; 261 } 262 263 static int xen_blkif_disconnect(struct xen_blkif *blkif) 264 { 265 struct pending_req *req, *n; 266 unsigned int j, r; 267 bool busy = false; 268 269 for (r = 0; r < blkif->nr_rings; r++) { 270 struct xen_blkif_ring *ring = &blkif->rings[r]; 271 unsigned int i = 0; 272 273 if (!ring->active) 274 continue; 275 276 if (ring->xenblkd) { 277 kthread_stop(ring->xenblkd); 278 wake_up(&ring->shutdown_wq); 279 } 280 281 /* The above kthread_stop() guarantees that at this point we 282 * don't have any discard_io or other_io requests. So, checking 283 * for inflight IO is enough. 284 */ 285 if (atomic_read(&ring->inflight) > 0) { 286 busy = true; 287 continue; 288 } 289 290 if (ring->irq) { 291 unbind_from_irqhandler(ring->irq, ring); 292 ring->irq = 0; 293 } 294 295 if (ring->blk_rings.common.sring) { 296 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring); 297 ring->blk_rings.common.sring = NULL; 298 } 299 300 /* Remove all persistent grants and the cache of ballooned pages. */ 301 xen_blkbk_free_caches(ring); 302 303 /* Check that there is no request in use */ 304 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) { 305 list_del(&req->free_list); 306 307 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) 308 kfree(req->segments[j]); 309 310 for (j = 0; j < MAX_INDIRECT_PAGES; j++) 311 kfree(req->indirect_pages[j]); 312 313 kfree(req); 314 i++; 315 } 316 317 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0); 318 BUG_ON(!list_empty(&ring->persistent_purge_list)); 319 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts)); 320 BUG_ON(!list_empty(&ring->free_pages)); 321 BUG_ON(ring->free_pages_num != 0); 322 BUG_ON(ring->persistent_gnt_c != 0); 323 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages)); 324 ring->active = false; 325 } 326 if (busy) 327 return -EBUSY; 328 329 blkif->nr_ring_pages = 0; 330 /* 331 * blkif->rings was allocated in connect_ring, so we should free it in 332 * here. 333 */ 334 kfree(blkif->rings); 335 blkif->rings = NULL; 336 blkif->nr_rings = 0; 337 338 return 0; 339 } 340 341 static void xen_blkif_free(struct xen_blkif *blkif) 342 { 343 WARN_ON(xen_blkif_disconnect(blkif)); 344 xen_vbd_free(&blkif->vbd); 345 kfree(blkif->be->mode); 346 kfree(blkif->be); 347 348 /* Make sure everything is drained before shutting down */ 349 kmem_cache_free(xen_blkif_cachep, blkif); 350 module_put(THIS_MODULE); 351 } 352 353 int __init xen_blkif_interface_init(void) 354 { 355 xen_blkif_cachep = kmem_cache_create("blkif_cache", 356 sizeof(struct xen_blkif), 357 0, 0, NULL); 358 if (!xen_blkif_cachep) 359 return -ENOMEM; 360 361 return 0; 362 } 363 364 void xen_blkif_interface_fini(void) 365 { 366 kmem_cache_destroy(xen_blkif_cachep); 367 xen_blkif_cachep = NULL; 368 } 369 370 /* 371 * sysfs interface for VBD I/O requests 372 */ 373 374 #define VBD_SHOW_ALLRING(name, format) \ 375 static ssize_t show_##name(struct device *_dev, \ 376 struct device_attribute *attr, \ 377 char *buf) \ 378 { \ 379 struct xenbus_device *dev = to_xenbus_device(_dev); \ 380 struct backend_info *be = dev_get_drvdata(&dev->dev); \ 381 struct xen_blkif *blkif = be->blkif; \ 382 unsigned int i; \ 383 unsigned long long result = 0; \ 384 \ 385 if (!blkif->rings) \ 386 goto out; \ 387 \ 388 for (i = 0; i < blkif->nr_rings; i++) { \ 389 struct xen_blkif_ring *ring = &blkif->rings[i]; \ 390 \ 391 result += ring->st_##name; \ 392 } \ 393 \ 394 out: \ 395 return sprintf(buf, format, result); \ 396 } \ 397 static DEVICE_ATTR(name, 0444, show_##name, NULL) 398 399 VBD_SHOW_ALLRING(oo_req, "%llu\n"); 400 VBD_SHOW_ALLRING(rd_req, "%llu\n"); 401 VBD_SHOW_ALLRING(wr_req, "%llu\n"); 402 VBD_SHOW_ALLRING(f_req, "%llu\n"); 403 VBD_SHOW_ALLRING(ds_req, "%llu\n"); 404 VBD_SHOW_ALLRING(rd_sect, "%llu\n"); 405 VBD_SHOW_ALLRING(wr_sect, "%llu\n"); 406 407 static struct attribute *xen_vbdstat_attrs[] = { 408 &dev_attr_oo_req.attr, 409 &dev_attr_rd_req.attr, 410 &dev_attr_wr_req.attr, 411 &dev_attr_f_req.attr, 412 &dev_attr_ds_req.attr, 413 &dev_attr_rd_sect.attr, 414 &dev_attr_wr_sect.attr, 415 NULL 416 }; 417 418 static const struct attribute_group xen_vbdstat_group = { 419 .name = "statistics", 420 .attrs = xen_vbdstat_attrs, 421 }; 422 423 #define VBD_SHOW(name, format, args...) \ 424 static ssize_t show_##name(struct device *_dev, \ 425 struct device_attribute *attr, \ 426 char *buf) \ 427 { \ 428 struct xenbus_device *dev = to_xenbus_device(_dev); \ 429 struct backend_info *be = dev_get_drvdata(&dev->dev); \ 430 \ 431 return sprintf(buf, format, ##args); \ 432 } \ 433 static DEVICE_ATTR(name, 0444, show_##name, NULL) 434 435 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); 436 VBD_SHOW(mode, "%s\n", be->mode); 437 438 static int xenvbd_sysfs_addif(struct xenbus_device *dev) 439 { 440 int error; 441 442 error = device_create_file(&dev->dev, &dev_attr_physical_device); 443 if (error) 444 goto fail1; 445 446 error = device_create_file(&dev->dev, &dev_attr_mode); 447 if (error) 448 goto fail2; 449 450 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); 451 if (error) 452 goto fail3; 453 454 return 0; 455 456 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 457 fail2: device_remove_file(&dev->dev, &dev_attr_mode); 458 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); 459 return error; 460 } 461 462 static void xenvbd_sysfs_delif(struct xenbus_device *dev) 463 { 464 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 465 device_remove_file(&dev->dev, &dev_attr_mode); 466 device_remove_file(&dev->dev, &dev_attr_physical_device); 467 } 468 469 static void xen_vbd_free(struct xen_vbd *vbd) 470 { 471 if (vbd->bdev) 472 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); 473 vbd->bdev = NULL; 474 } 475 476 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, 477 unsigned major, unsigned minor, int readonly, 478 int cdrom) 479 { 480 struct xen_vbd *vbd; 481 struct block_device *bdev; 482 struct request_queue *q; 483 484 vbd = &blkif->vbd; 485 vbd->handle = handle; 486 vbd->readonly = readonly; 487 vbd->type = 0; 488 489 vbd->pdevice = MKDEV(major, minor); 490 491 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? 492 FMODE_READ : FMODE_WRITE, NULL); 493 494 if (IS_ERR(bdev)) { 495 pr_warn("xen_vbd_create: device %08x could not be opened\n", 496 vbd->pdevice); 497 return -ENOENT; 498 } 499 500 vbd->bdev = bdev; 501 if (vbd->bdev->bd_disk == NULL) { 502 pr_warn("xen_vbd_create: device %08x doesn't exist\n", 503 vbd->pdevice); 504 xen_vbd_free(vbd); 505 return -ENOENT; 506 } 507 vbd->size = vbd_sz(vbd); 508 509 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) 510 vbd->type |= VDISK_CDROM; 511 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) 512 vbd->type |= VDISK_REMOVABLE; 513 514 q = bdev_get_queue(bdev); 515 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags)) 516 vbd->flush_support = true; 517 518 if (q && blk_queue_secure_erase(q)) 519 vbd->discard_secure = true; 520 521 pr_debug("Successful creation of handle=%04x (dom=%u)\n", 522 handle, blkif->domid); 523 return 0; 524 } 525 526 static int xen_blkbk_remove(struct xenbus_device *dev) 527 { 528 struct backend_info *be = dev_get_drvdata(&dev->dev); 529 530 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 531 532 if (be->major || be->minor) 533 xenvbd_sysfs_delif(dev); 534 535 if (be->backend_watch.node) { 536 unregister_xenbus_watch(&be->backend_watch); 537 kfree(be->backend_watch.node); 538 be->backend_watch.node = NULL; 539 } 540 541 dev_set_drvdata(&dev->dev, NULL); 542 543 if (be->blkif) { 544 xen_blkif_disconnect(be->blkif); 545 546 /* Put the reference we set in xen_blkif_alloc(). */ 547 xen_blkif_put(be->blkif); 548 } 549 550 return 0; 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; 574 struct request_queue *q = bdev_get_queue(bdev); 575 576 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1)) 577 return; 578 579 if (blk_queue_discard(q)) { 580 err = xenbus_printf(xbt, dev->nodename, 581 "discard-granularity", "%u", 582 q->limits.discard_granularity); 583 if (err) { 584 dev_warn(&dev->dev, "writing discard-granularity (%d)", err); 585 return; 586 } 587 err = xenbus_printf(xbt, dev->nodename, 588 "discard-alignment", "%u", 589 q->limits.discard_alignment); 590 if (err) { 591 dev_warn(&dev->dev, "writing discard-alignment (%d)", err); 592 return; 593 } 594 state = 1; 595 /* Optional. */ 596 err = xenbus_printf(xbt, dev->nodename, 597 "discard-secure", "%d", 598 blkif->vbd.discard_secure); 599 if (err) { 600 dev_warn(&dev->dev, "writing discard-secure (%d)", err); 601 return; 602 } 603 } 604 err = xenbus_printf(xbt, dev->nodename, "feature-discard", 605 "%d", state); 606 if (err) 607 dev_warn(&dev->dev, "writing feature-discard (%d)", err); 608 } 609 610 int xen_blkbk_barrier(struct xenbus_transaction xbt, 611 struct backend_info *be, int state) 612 { 613 struct xenbus_device *dev = be->dev; 614 int err; 615 616 err = xenbus_printf(xbt, dev->nodename, "feature-barrier", 617 "%d", state); 618 if (err) 619 dev_warn(&dev->dev, "writing feature-barrier (%d)", err); 620 621 return err; 622 } 623 624 /* 625 * Entry point to this code when a new device is created. Allocate the basic 626 * structures, and watch the store waiting for the hotplug scripts to tell us 627 * the device's physical major and minor numbers. Switch to InitWait. 628 */ 629 static int xen_blkbk_probe(struct xenbus_device *dev, 630 const struct xenbus_device_id *id) 631 { 632 int err; 633 struct backend_info *be = kzalloc(sizeof(struct backend_info), 634 GFP_KERNEL); 635 636 /* match the pr_debug in xen_blkbk_remove */ 637 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); 638 639 if (!be) { 640 xenbus_dev_fatal(dev, -ENOMEM, 641 "allocating backend structure"); 642 return -ENOMEM; 643 } 644 be->dev = dev; 645 dev_set_drvdata(&dev->dev, be); 646 647 be->blkif = xen_blkif_alloc(dev->otherend_id); 648 if (IS_ERR(be->blkif)) { 649 err = PTR_ERR(be->blkif); 650 be->blkif = NULL; 651 xenbus_dev_fatal(dev, err, "creating block interface"); 652 goto fail; 653 } 654 655 err = xenbus_printf(XBT_NIL, dev->nodename, 656 "feature-max-indirect-segments", "%u", 657 MAX_INDIRECT_SEGMENTS); 658 if (err) 659 dev_warn(&dev->dev, 660 "writing %s/feature-max-indirect-segments (%d)", 661 dev->nodename, err); 662 663 /* Multi-queue: advertise how many queues are supported by us.*/ 664 err = xenbus_printf(XBT_NIL, dev->nodename, 665 "multi-queue-max-queues", "%u", xenblk_max_queues); 666 if (err) 667 pr_warn("Error writing multi-queue-max-queues\n"); 668 669 /* setup back pointer */ 670 be->blkif->be = be; 671 672 err = xenbus_watch_pathfmt(dev, &be->backend_watch, 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", 1); 909 if (err) { 910 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", 911 dev->nodename); 912 goto abort; 913 } 914 915 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", 916 (unsigned long long)vbd_sz(&be->blkif->vbd)); 917 if (err) { 918 xenbus_dev_fatal(dev, err, "writing %s/sectors", 919 dev->nodename); 920 goto abort; 921 } 922 923 /* FIXME: use a typename instead */ 924 err = xenbus_printf(xbt, dev->nodename, "info", "%u", 925 be->blkif->vbd.type | 926 (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); 927 if (err) { 928 xenbus_dev_fatal(dev, err, "writing %s/info", 929 dev->nodename); 930 goto abort; 931 } 932 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", 933 (unsigned long) 934 bdev_logical_block_size(be->blkif->vbd.bdev)); 935 if (err) { 936 xenbus_dev_fatal(dev, err, "writing %s/sector-size", 937 dev->nodename); 938 goto abort; 939 } 940 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u", 941 bdev_physical_block_size(be->blkif->vbd.bdev)); 942 if (err) 943 xenbus_dev_error(dev, err, "writing %s/physical-sector-size", 944 dev->nodename); 945 946 err = xenbus_transaction_end(xbt, 0); 947 if (err == -EAGAIN) 948 goto again; 949 if (err) 950 xenbus_dev_fatal(dev, err, "ending transaction"); 951 952 err = xenbus_switch_state(dev, XenbusStateConnected); 953 if (err) 954 xenbus_dev_fatal(dev, err, "%s: switching to Connected state", 955 dev->nodename); 956 957 return; 958 abort: 959 xenbus_transaction_end(xbt, 1); 960 } 961 962 /* 963 * Each ring may have multi pages, depends on "ring-page-order". 964 */ 965 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir) 966 { 967 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; 968 struct pending_req *req, *n; 969 int err, i, j; 970 struct xen_blkif *blkif = ring->blkif; 971 struct xenbus_device *dev = blkif->be->dev; 972 unsigned int nr_grefs, evtchn; 973 974 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u", 975 &evtchn); 976 if (err != 1) { 977 err = -EINVAL; 978 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir); 979 return err; 980 } 981 982 nr_grefs = blkif->nr_ring_pages; 983 984 if (unlikely(!nr_grefs)) { 985 WARN_ON(true); 986 return -EINVAL; 987 } 988 989 for (i = 0; i < nr_grefs; i++) { 990 char ring_ref_name[RINGREF_NAME_LEN]; 991 992 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); 993 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name, 994 "%u", &ring_ref[i]); 995 996 if (err != 1) { 997 if (nr_grefs == 1) 998 break; 999 1000 err = -EINVAL; 1001 xenbus_dev_fatal(dev, err, "reading %s/%s", 1002 dir, ring_ref_name); 1003 return err; 1004 } 1005 } 1006 1007 if (err != 1) { 1008 WARN_ON(nr_grefs != 1); 1009 1010 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", 1011 &ring_ref[0]); 1012 if (err != 1) { 1013 err = -EINVAL; 1014 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir); 1015 return err; 1016 } 1017 } 1018 1019 err = -ENOMEM; 1020 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) { 1021 req = kzalloc(sizeof(*req), GFP_KERNEL); 1022 if (!req) 1023 goto fail; 1024 list_add_tail(&req->free_list, &ring->pending_free); 1025 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 1026 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL); 1027 if (!req->segments[j]) 1028 goto fail; 1029 } 1030 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 1031 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]), 1032 GFP_KERNEL); 1033 if (!req->indirect_pages[j]) 1034 goto fail; 1035 } 1036 } 1037 1038 /* Map the shared frame, irq etc. */ 1039 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn); 1040 if (err) { 1041 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn); 1042 goto fail; 1043 } 1044 1045 return 0; 1046 1047 fail: 1048 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) { 1049 list_del(&req->free_list); 1050 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 1051 if (!req->segments[j]) 1052 break; 1053 kfree(req->segments[j]); 1054 } 1055 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 1056 if (!req->indirect_pages[j]) 1057 break; 1058 kfree(req->indirect_pages[j]); 1059 } 1060 kfree(req); 1061 } 1062 return err; 1063 } 1064 1065 static int connect_ring(struct backend_info *be) 1066 { 1067 struct xenbus_device *dev = be->dev; 1068 struct xen_blkif *blkif = be->blkif; 1069 unsigned int pers_grants; 1070 char protocol[64] = ""; 1071 int err, i; 1072 char *xspath; 1073 size_t xspathsize; 1074 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */ 1075 unsigned int requested_num_queues = 0; 1076 unsigned int ring_page_order; 1077 1078 pr_debug("%s %s\n", __func__, dev->otherend); 1079 1080 blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT; 1081 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol", 1082 "%63s", protocol); 1083 if (err <= 0) 1084 strcpy(protocol, "unspecified, assuming default"); 1085 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) 1086 blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; 1087 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) 1088 blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; 1089 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) 1090 blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; 1091 else { 1092 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); 1093 return -ENOSYS; 1094 } 1095 pers_grants = xenbus_read_unsigned(dev->otherend, "feature-persistent", 1096 0); 1097 blkif->vbd.feature_gnt_persistent = pers_grants; 1098 blkif->vbd.overflow_max_grants = 0; 1099 1100 /* 1101 * Read the number of hardware queues from frontend. 1102 */ 1103 requested_num_queues = xenbus_read_unsigned(dev->otherend, 1104 "multi-queue-num-queues", 1105 1); 1106 if (requested_num_queues > xenblk_max_queues 1107 || requested_num_queues == 0) { 1108 /* Buggy or malicious guest. */ 1109 xenbus_dev_fatal(dev, err, 1110 "guest requested %u queues, exceeding the maximum of %u.", 1111 requested_num_queues, xenblk_max_queues); 1112 return -ENOSYS; 1113 } 1114 blkif->nr_rings = requested_num_queues; 1115 if (xen_blkif_alloc_rings(blkif)) 1116 return -ENOMEM; 1117 1118 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename, 1119 blkif->nr_rings, blkif->blk_protocol, protocol, 1120 pers_grants ? "persistent grants" : ""); 1121 1122 ring_page_order = xenbus_read_unsigned(dev->otherend, 1123 "ring-page-order", 0); 1124 1125 if (ring_page_order > xen_blkif_max_ring_order) { 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 blkif->nr_ring_pages = 1 << ring_page_order; 1135 1136 if (blkif->nr_rings == 1) 1137 return read_per_ring_refs(&blkif->rings[0], dev->otherend); 1138 else { 1139 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size; 1140 xspath = kmalloc(xspathsize, GFP_KERNEL); 1141 if (!xspath) { 1142 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references"); 1143 return -ENOMEM; 1144 } 1145 1146 for (i = 0; i < blkif->nr_rings; i++) { 1147 memset(xspath, 0, xspathsize); 1148 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i); 1149 err = read_per_ring_refs(&blkif->rings[i], xspath); 1150 if (err) { 1151 kfree(xspath); 1152 return err; 1153 } 1154 } 1155 kfree(xspath); 1156 } 1157 return 0; 1158 } 1159 1160 static const struct xenbus_device_id xen_blkbk_ids[] = { 1161 { "vbd" }, 1162 { "" } 1163 }; 1164 1165 static struct xenbus_driver xen_blkbk_driver = { 1166 .ids = xen_blkbk_ids, 1167 .probe = xen_blkbk_probe, 1168 .remove = xen_blkbk_remove, 1169 .otherend_changed = frontend_changed, 1170 .allow_rebind = true, 1171 .reclaim_memory = reclaim_memory, 1172 }; 1173 1174 int xen_blkif_xenbus_init(void) 1175 { 1176 return xenbus_register_backend(&xen_blkbk_driver); 1177 } 1178 1179 void xen_blkif_xenbus_fini(void) 1180 { 1181 xenbus_unregister_driver(&xen_blkbk_driver); 1182 } 1183