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