1 /* Xenbus code for blkif backend 2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au> 3 Copyright (C) 2005 XenSource Ltd 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 */ 16 17 #include <stdarg.h> 18 #include <linux/module.h> 19 #include <linux/kthread.h> 20 #include <xen/events.h> 21 #include <xen/grant_table.h> 22 #include "common.h" 23 24 struct backend_info { 25 struct xenbus_device *dev; 26 struct xen_blkif *blkif; 27 struct xenbus_watch backend_watch; 28 unsigned major; 29 unsigned minor; 30 char *mode; 31 }; 32 33 static struct kmem_cache *xen_blkif_cachep; 34 static void connect(struct backend_info *); 35 static int connect_ring(struct backend_info *); 36 static void backend_changed(struct xenbus_watch *, const char **, 37 unsigned int); 38 static void xen_blkif_free(struct xen_blkif *blkif); 39 static void xen_vbd_free(struct xen_vbd *vbd); 40 41 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) 42 { 43 return be->dev; 44 } 45 46 /* 47 * The last request could free the device from softirq context and 48 * xen_blkif_free() can sleep. 49 */ 50 static void xen_blkif_deferred_free(struct work_struct *work) 51 { 52 struct xen_blkif *blkif; 53 54 blkif = container_of(work, struct xen_blkif, free_work); 55 xen_blkif_free(blkif); 56 } 57 58 static int blkback_name(struct xen_blkif *blkif, char *buf) 59 { 60 char *devpath, *devname; 61 struct xenbus_device *dev = blkif->be->dev; 62 63 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); 64 if (IS_ERR(devpath)) 65 return PTR_ERR(devpath); 66 67 devname = strstr(devpath, "/dev/"); 68 if (devname != NULL) 69 devname += strlen("/dev/"); 70 else 71 devname = devpath; 72 73 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname); 74 kfree(devpath); 75 76 return 0; 77 } 78 79 static void xen_update_blkif_status(struct xen_blkif *blkif) 80 { 81 int err; 82 char name[TASK_COMM_LEN]; 83 84 /* Not ready to connect? */ 85 if (!blkif->irq || !blkif->vbd.bdev) 86 return; 87 88 /* Already connected? */ 89 if (blkif->be->dev->state == XenbusStateConnected) 90 return; 91 92 /* Attempt to connect: exit if we fail to. */ 93 connect(blkif->be); 94 if (blkif->be->dev->state != XenbusStateConnected) 95 return; 96 97 err = blkback_name(blkif, name); 98 if (err) { 99 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); 100 return; 101 } 102 103 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); 104 if (err) { 105 xenbus_dev_error(blkif->be->dev, err, "block flush"); 106 return; 107 } 108 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); 109 110 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name); 111 if (IS_ERR(blkif->xenblkd)) { 112 err = PTR_ERR(blkif->xenblkd); 113 blkif->xenblkd = NULL; 114 xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); 115 return; 116 } 117 } 118 119 static struct xen_blkif *xen_blkif_alloc(domid_t domid) 120 { 121 struct xen_blkif *blkif; 122 struct pending_req *req, *n; 123 int i, j; 124 125 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST); 126 127 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL); 128 if (!blkif) 129 return ERR_PTR(-ENOMEM); 130 131 blkif->domid = domid; 132 spin_lock_init(&blkif->blk_ring_lock); 133 atomic_set(&blkif->refcnt, 1); 134 init_waitqueue_head(&blkif->wq); 135 init_completion(&blkif->drain_complete); 136 atomic_set(&blkif->drain, 0); 137 blkif->st_print = jiffies; 138 blkif->persistent_gnts.rb_node = NULL; 139 spin_lock_init(&blkif->free_pages_lock); 140 INIT_LIST_HEAD(&blkif->free_pages); 141 INIT_LIST_HEAD(&blkif->persistent_purge_list); 142 blkif->free_pages_num = 0; 143 atomic_set(&blkif->persistent_gnt_in_use, 0); 144 atomic_set(&blkif->inflight, 0); 145 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants); 146 147 INIT_LIST_HEAD(&blkif->pending_free); 148 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); 149 150 for (i = 0; i < XEN_BLKIF_REQS; i++) { 151 req = kzalloc(sizeof(*req), GFP_KERNEL); 152 if (!req) 153 goto fail; 154 list_add_tail(&req->free_list, 155 &blkif->pending_free); 156 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 157 req->segments[j] = kzalloc(sizeof(*req->segments[0]), 158 GFP_KERNEL); 159 if (!req->segments[j]) 160 goto fail; 161 } 162 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 163 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]), 164 GFP_KERNEL); 165 if (!req->indirect_pages[j]) 166 goto fail; 167 } 168 } 169 spin_lock_init(&blkif->pending_free_lock); 170 init_waitqueue_head(&blkif->pending_free_wq); 171 init_waitqueue_head(&blkif->shutdown_wq); 172 173 return blkif; 174 175 fail: 176 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) { 177 list_del(&req->free_list); 178 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { 179 if (!req->segments[j]) 180 break; 181 kfree(req->segments[j]); 182 } 183 for (j = 0; j < MAX_INDIRECT_PAGES; j++) { 184 if (!req->indirect_pages[j]) 185 break; 186 kfree(req->indirect_pages[j]); 187 } 188 kfree(req); 189 } 190 191 kmem_cache_free(xen_blkif_cachep, blkif); 192 193 return ERR_PTR(-ENOMEM); 194 } 195 196 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, 197 unsigned int evtchn) 198 { 199 int err; 200 201 /* Already connected through? */ 202 if (blkif->irq) 203 return 0; 204 205 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring); 206 if (err < 0) 207 return err; 208 209 switch (blkif->blk_protocol) { 210 case BLKIF_PROTOCOL_NATIVE: 211 { 212 struct blkif_sring *sring; 213 sring = (struct blkif_sring *)blkif->blk_ring; 214 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); 215 break; 216 } 217 case BLKIF_PROTOCOL_X86_32: 218 { 219 struct blkif_x86_32_sring *sring_x86_32; 220 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; 221 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); 222 break; 223 } 224 case BLKIF_PROTOCOL_X86_64: 225 { 226 struct blkif_x86_64_sring *sring_x86_64; 227 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; 228 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); 229 break; 230 } 231 default: 232 BUG(); 233 } 234 235 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, 236 xen_blkif_be_int, 0, 237 "blkif-backend", blkif); 238 if (err < 0) { 239 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); 240 blkif->blk_rings.common.sring = NULL; 241 return err; 242 } 243 blkif->irq = err; 244 245 return 0; 246 } 247 248 static int xen_blkif_disconnect(struct xen_blkif *blkif) 249 { 250 if (blkif->xenblkd) { 251 kthread_stop(blkif->xenblkd); 252 wake_up(&blkif->shutdown_wq); 253 blkif->xenblkd = NULL; 254 } 255 256 /* The above kthread_stop() guarantees that at this point we 257 * don't have any discard_io or other_io requests. So, checking 258 * for inflight IO is enough. 259 */ 260 if (atomic_read(&blkif->inflight) > 0) 261 return -EBUSY; 262 263 if (blkif->irq) { 264 unbind_from_irqhandler(blkif->irq, blkif); 265 blkif->irq = 0; 266 } 267 268 if (blkif->blk_rings.common.sring) { 269 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); 270 blkif->blk_rings.common.sring = NULL; 271 } 272 273 return 0; 274 } 275 276 static void xen_blkif_free(struct xen_blkif *blkif) 277 { 278 struct pending_req *req, *n; 279 int i = 0, j; 280 281 xen_blkif_disconnect(blkif); 282 xen_vbd_free(&blkif->vbd); 283 284 /* Remove all persistent grants and the cache of ballooned pages. */ 285 xen_blkbk_free_caches(blkif); 286 287 /* Make sure everything is drained before shutting down */ 288 BUG_ON(blkif->persistent_gnt_c != 0); 289 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0); 290 BUG_ON(blkif->free_pages_num != 0); 291 BUG_ON(!list_empty(&blkif->persistent_purge_list)); 292 BUG_ON(!list_empty(&blkif->free_pages)); 293 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts)); 294 295 /* Check that there is no request in use */ 296 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) { 297 list_del(&req->free_list); 298 299 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) 300 kfree(req->segments[j]); 301 302 for (j = 0; j < MAX_INDIRECT_PAGES; j++) 303 kfree(req->indirect_pages[j]); 304 305 kfree(req); 306 i++; 307 } 308 309 WARN_ON(i != XEN_BLKIF_REQS); 310 311 kmem_cache_free(xen_blkif_cachep, blkif); 312 } 313 314 int __init xen_blkif_interface_init(void) 315 { 316 xen_blkif_cachep = kmem_cache_create("blkif_cache", 317 sizeof(struct xen_blkif), 318 0, 0, NULL); 319 if (!xen_blkif_cachep) 320 return -ENOMEM; 321 322 return 0; 323 } 324 325 /* 326 * sysfs interface for VBD I/O requests 327 */ 328 329 #define VBD_SHOW(name, format, args...) \ 330 static ssize_t show_##name(struct device *_dev, \ 331 struct device_attribute *attr, \ 332 char *buf) \ 333 { \ 334 struct xenbus_device *dev = to_xenbus_device(_dev); \ 335 struct backend_info *be = dev_get_drvdata(&dev->dev); \ 336 \ 337 return sprintf(buf, format, ##args); \ 338 } \ 339 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) 340 341 VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req); 342 VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req); 343 VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req); 344 VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req); 345 VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req); 346 VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect); 347 VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect); 348 349 static struct attribute *xen_vbdstat_attrs[] = { 350 &dev_attr_oo_req.attr, 351 &dev_attr_rd_req.attr, 352 &dev_attr_wr_req.attr, 353 &dev_attr_f_req.attr, 354 &dev_attr_ds_req.attr, 355 &dev_attr_rd_sect.attr, 356 &dev_attr_wr_sect.attr, 357 NULL 358 }; 359 360 static struct attribute_group xen_vbdstat_group = { 361 .name = "statistics", 362 .attrs = xen_vbdstat_attrs, 363 }; 364 365 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); 366 VBD_SHOW(mode, "%s\n", be->mode); 367 368 static int xenvbd_sysfs_addif(struct xenbus_device *dev) 369 { 370 int error; 371 372 error = device_create_file(&dev->dev, &dev_attr_physical_device); 373 if (error) 374 goto fail1; 375 376 error = device_create_file(&dev->dev, &dev_attr_mode); 377 if (error) 378 goto fail2; 379 380 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); 381 if (error) 382 goto fail3; 383 384 return 0; 385 386 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 387 fail2: device_remove_file(&dev->dev, &dev_attr_mode); 388 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); 389 return error; 390 } 391 392 static void xenvbd_sysfs_delif(struct xenbus_device *dev) 393 { 394 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); 395 device_remove_file(&dev->dev, &dev_attr_mode); 396 device_remove_file(&dev->dev, &dev_attr_physical_device); 397 } 398 399 400 static void xen_vbd_free(struct xen_vbd *vbd) 401 { 402 if (vbd->bdev) 403 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); 404 vbd->bdev = NULL; 405 } 406 407 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, 408 unsigned major, unsigned minor, int readonly, 409 int cdrom) 410 { 411 struct xen_vbd *vbd; 412 struct block_device *bdev; 413 struct request_queue *q; 414 415 vbd = &blkif->vbd; 416 vbd->handle = handle; 417 vbd->readonly = readonly; 418 vbd->type = 0; 419 420 vbd->pdevice = MKDEV(major, minor); 421 422 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? 423 FMODE_READ : FMODE_WRITE, NULL); 424 425 if (IS_ERR(bdev)) { 426 DPRINTK("xen_vbd_create: device %08x could not be opened.\n", 427 vbd->pdevice); 428 return -ENOENT; 429 } 430 431 vbd->bdev = bdev; 432 if (vbd->bdev->bd_disk == NULL) { 433 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n", 434 vbd->pdevice); 435 xen_vbd_free(vbd); 436 return -ENOENT; 437 } 438 vbd->size = vbd_sz(vbd); 439 440 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) 441 vbd->type |= VDISK_CDROM; 442 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) 443 vbd->type |= VDISK_REMOVABLE; 444 445 q = bdev_get_queue(bdev); 446 if (q && q->flush_flags) 447 vbd->flush_support = true; 448 449 if (q && blk_queue_secdiscard(q)) 450 vbd->discard_secure = true; 451 452 DPRINTK("Successful creation of handle=%04x (dom=%u)\n", 453 handle, blkif->domid); 454 return 0; 455 } 456 static int xen_blkbk_remove(struct xenbus_device *dev) 457 { 458 struct backend_info *be = dev_get_drvdata(&dev->dev); 459 460 DPRINTK(""); 461 462 if (be->major || be->minor) 463 xenvbd_sysfs_delif(dev); 464 465 if (be->backend_watch.node) { 466 unregister_xenbus_watch(&be->backend_watch); 467 kfree(be->backend_watch.node); 468 be->backend_watch.node = NULL; 469 } 470 471 dev_set_drvdata(&dev->dev, NULL); 472 473 if (be->blkif) { 474 xen_blkif_disconnect(be->blkif); 475 xen_blkif_put(be->blkif); 476 } 477 478 kfree(be->mode); 479 kfree(be); 480 return 0; 481 } 482 483 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, 484 struct backend_info *be, int state) 485 { 486 struct xenbus_device *dev = be->dev; 487 int err; 488 489 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", 490 "%d", state); 491 if (err) 492 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err); 493 494 return err; 495 } 496 497 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) 498 { 499 struct xenbus_device *dev = be->dev; 500 struct xen_blkif *blkif = be->blkif; 501 int err; 502 int state = 0, discard_enable; 503 struct block_device *bdev = be->blkif->vbd.bdev; 504 struct request_queue *q = bdev_get_queue(bdev); 505 506 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d", 507 &discard_enable); 508 if (err == 1 && !discard_enable) 509 return; 510 511 if (blk_queue_discard(q)) { 512 err = xenbus_printf(xbt, dev->nodename, 513 "discard-granularity", "%u", 514 q->limits.discard_granularity); 515 if (err) { 516 dev_warn(&dev->dev, "writing discard-granularity (%d)", err); 517 return; 518 } 519 err = xenbus_printf(xbt, dev->nodename, 520 "discard-alignment", "%u", 521 q->limits.discard_alignment); 522 if (err) { 523 dev_warn(&dev->dev, "writing discard-alignment (%d)", err); 524 return; 525 } 526 state = 1; 527 /* Optional. */ 528 err = xenbus_printf(xbt, dev->nodename, 529 "discard-secure", "%d", 530 blkif->vbd.discard_secure); 531 if (err) { 532 dev_warn(&dev->dev, "writing discard-secure (%d)", err); 533 return; 534 } 535 } 536 err = xenbus_printf(xbt, dev->nodename, "feature-discard", 537 "%d", state); 538 if (err) 539 dev_warn(&dev->dev, "writing feature-discard (%d)", err); 540 } 541 int xen_blkbk_barrier(struct xenbus_transaction xbt, 542 struct backend_info *be, int state) 543 { 544 struct xenbus_device *dev = be->dev; 545 int err; 546 547 err = xenbus_printf(xbt, dev->nodename, "feature-barrier", 548 "%d", state); 549 if (err) 550 dev_warn(&dev->dev, "writing feature-barrier (%d)", err); 551 552 return err; 553 } 554 555 /* 556 * Entry point to this code when a new device is created. Allocate the basic 557 * structures, and watch the store waiting for the hotplug scripts to tell us 558 * the device's physical major and minor numbers. Switch to InitWait. 559 */ 560 static int xen_blkbk_probe(struct xenbus_device *dev, 561 const struct xenbus_device_id *id) 562 { 563 int err; 564 struct backend_info *be = kzalloc(sizeof(struct backend_info), 565 GFP_KERNEL); 566 if (!be) { 567 xenbus_dev_fatal(dev, -ENOMEM, 568 "allocating backend structure"); 569 return -ENOMEM; 570 } 571 be->dev = dev; 572 dev_set_drvdata(&dev->dev, be); 573 574 be->blkif = xen_blkif_alloc(dev->otherend_id); 575 if (IS_ERR(be->blkif)) { 576 err = PTR_ERR(be->blkif); 577 be->blkif = NULL; 578 xenbus_dev_fatal(dev, err, "creating block interface"); 579 goto fail; 580 } 581 582 /* setup back pointer */ 583 be->blkif->be = be; 584 585 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, 586 "%s/%s", dev->nodename, "physical-device"); 587 if (err) 588 goto fail; 589 590 err = xenbus_switch_state(dev, XenbusStateInitWait); 591 if (err) 592 goto fail; 593 594 return 0; 595 596 fail: 597 DPRINTK("failed"); 598 xen_blkbk_remove(dev); 599 return err; 600 } 601 602 603 /* 604 * Callback received when the hotplug scripts have placed the physical-device 605 * node. Read it and the mode node, and create a vbd. If the frontend is 606 * ready, connect. 607 */ 608 static void backend_changed(struct xenbus_watch *watch, 609 const char **vec, unsigned int len) 610 { 611 int err; 612 unsigned major; 613 unsigned minor; 614 struct backend_info *be 615 = container_of(watch, struct backend_info, backend_watch); 616 struct xenbus_device *dev = be->dev; 617 int cdrom = 0; 618 unsigned long handle; 619 char *device_type; 620 621 DPRINTK(""); 622 623 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", 624 &major, &minor); 625 if (XENBUS_EXIST_ERR(err)) { 626 /* 627 * Since this watch will fire once immediately after it is 628 * registered, we expect this. Ignore it, and wait for the 629 * hotplug scripts. 630 */ 631 return; 632 } 633 if (err != 2) { 634 xenbus_dev_fatal(dev, err, "reading physical-device"); 635 return; 636 } 637 638 if (be->major | be->minor) { 639 if (be->major != major || be->minor != minor) 640 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n", 641 be->major, be->minor, major, minor); 642 return; 643 } 644 645 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); 646 if (IS_ERR(be->mode)) { 647 err = PTR_ERR(be->mode); 648 be->mode = NULL; 649 xenbus_dev_fatal(dev, err, "reading mode"); 650 return; 651 } 652 653 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); 654 if (!IS_ERR(device_type)) { 655 cdrom = strcmp(device_type, "cdrom") == 0; 656 kfree(device_type); 657 } 658 659 /* Front end dir is a number, which is used as the handle. */ 660 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle); 661 if (err) 662 return; 663 664 be->major = major; 665 be->minor = minor; 666 667 err = xen_vbd_create(be->blkif, handle, major, minor, 668 !strchr(be->mode, 'w'), cdrom); 669 670 if (err) 671 xenbus_dev_fatal(dev, err, "creating vbd structure"); 672 else { 673 err = xenvbd_sysfs_addif(dev); 674 if (err) { 675 xen_vbd_free(&be->blkif->vbd); 676 xenbus_dev_fatal(dev, err, "creating sysfs entries"); 677 } 678 } 679 680 if (err) { 681 kfree(be->mode); 682 be->mode = NULL; 683 be->major = 0; 684 be->minor = 0; 685 } else { 686 /* We're potentially connected now */ 687 xen_update_blkif_status(be->blkif); 688 } 689 } 690 691 692 /* 693 * Callback received when the frontend's state changes. 694 */ 695 static void frontend_changed(struct xenbus_device *dev, 696 enum xenbus_state frontend_state) 697 { 698 struct backend_info *be = dev_get_drvdata(&dev->dev); 699 int err; 700 701 DPRINTK("%s", xenbus_strstate(frontend_state)); 702 703 switch (frontend_state) { 704 case XenbusStateInitialising: 705 if (dev->state == XenbusStateClosed) { 706 pr_info(DRV_PFX "%s: prepare for reconnect\n", 707 dev->nodename); 708 xenbus_switch_state(dev, XenbusStateInitWait); 709 } 710 break; 711 712 case XenbusStateInitialised: 713 case XenbusStateConnected: 714 /* 715 * Ensure we connect even when two watches fire in 716 * close succession and we miss the intermediate value 717 * of frontend_state. 718 */ 719 if (dev->state == XenbusStateConnected) 720 break; 721 722 /* 723 * Enforce precondition before potential leak point. 724 * xen_blkif_disconnect() is idempotent. 725 */ 726 err = xen_blkif_disconnect(be->blkif); 727 if (err) { 728 xenbus_dev_fatal(dev, err, "pending I/O"); 729 break; 730 } 731 732 err = connect_ring(be); 733 if (err) 734 break; 735 xen_update_blkif_status(be->blkif); 736 break; 737 738 case XenbusStateClosing: 739 xenbus_switch_state(dev, XenbusStateClosing); 740 break; 741 742 case XenbusStateClosed: 743 xen_blkif_disconnect(be->blkif); 744 xenbus_switch_state(dev, XenbusStateClosed); 745 if (xenbus_dev_is_online(dev)) 746 break; 747 /* fall through if not online */ 748 case XenbusStateUnknown: 749 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ 750 device_unregister(&dev->dev); 751 break; 752 753 default: 754 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", 755 frontend_state); 756 break; 757 } 758 } 759 760 761 /* ** Connection ** */ 762 763 764 /* 765 * Write the physical details regarding the block device to the store, and 766 * switch to Connected state. 767 */ 768 static void connect(struct backend_info *be) 769 { 770 struct xenbus_transaction xbt; 771 int err; 772 struct xenbus_device *dev = be->dev; 773 774 DPRINTK("%s", dev->otherend); 775 776 /* Supply the information about the device the frontend needs */ 777 again: 778 err = xenbus_transaction_start(&xbt); 779 if (err) { 780 xenbus_dev_fatal(dev, err, "starting transaction"); 781 return; 782 } 783 784 /* If we can't advertise it is OK. */ 785 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); 786 787 xen_blkbk_discard(xbt, be); 788 789 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); 790 791 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1); 792 if (err) { 793 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", 794 dev->nodename); 795 goto abort; 796 } 797 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u", 798 MAX_INDIRECT_SEGMENTS); 799 if (err) 800 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)", 801 dev->nodename, err); 802 803 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", 804 (unsigned long long)vbd_sz(&be->blkif->vbd)); 805 if (err) { 806 xenbus_dev_fatal(dev, err, "writing %s/sectors", 807 dev->nodename); 808 goto abort; 809 } 810 811 /* FIXME: use a typename instead */ 812 err = xenbus_printf(xbt, dev->nodename, "info", "%u", 813 be->blkif->vbd.type | 814 (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); 815 if (err) { 816 xenbus_dev_fatal(dev, err, "writing %s/info", 817 dev->nodename); 818 goto abort; 819 } 820 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", 821 (unsigned long) 822 bdev_logical_block_size(be->blkif->vbd.bdev)); 823 if (err) { 824 xenbus_dev_fatal(dev, err, "writing %s/sector-size", 825 dev->nodename); 826 goto abort; 827 } 828 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u", 829 bdev_physical_block_size(be->blkif->vbd.bdev)); 830 if (err) 831 xenbus_dev_error(dev, err, "writing %s/physical-sector-size", 832 dev->nodename); 833 834 err = xenbus_transaction_end(xbt, 0); 835 if (err == -EAGAIN) 836 goto again; 837 if (err) 838 xenbus_dev_fatal(dev, err, "ending transaction"); 839 840 err = xenbus_switch_state(dev, XenbusStateConnected); 841 if (err) 842 xenbus_dev_fatal(dev, err, "%s: switching to Connected state", 843 dev->nodename); 844 845 return; 846 abort: 847 xenbus_transaction_end(xbt, 1); 848 } 849 850 851 static int connect_ring(struct backend_info *be) 852 { 853 struct xenbus_device *dev = be->dev; 854 unsigned long ring_ref; 855 unsigned int evtchn; 856 unsigned int pers_grants; 857 char protocol[64] = ""; 858 int err; 859 860 DPRINTK("%s", dev->otherend); 861 862 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", 863 &ring_ref, "event-channel", "%u", &evtchn, NULL); 864 if (err) { 865 xenbus_dev_fatal(dev, err, 866 "reading %s/ring-ref and event-channel", 867 dev->otherend); 868 return err; 869 } 870 871 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; 872 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", 873 "%63s", protocol, NULL); 874 if (err) 875 strcpy(protocol, "unspecified, assuming native"); 876 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) 877 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; 878 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) 879 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; 880 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) 881 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; 882 else { 883 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); 884 return -1; 885 } 886 err = xenbus_gather(XBT_NIL, dev->otherend, 887 "feature-persistent", "%u", 888 &pers_grants, NULL); 889 if (err) 890 pers_grants = 0; 891 892 be->blkif->vbd.feature_gnt_persistent = pers_grants; 893 be->blkif->vbd.overflow_max_grants = 0; 894 895 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n", 896 ring_ref, evtchn, be->blkif->blk_protocol, protocol, 897 pers_grants ? "persistent grants" : ""); 898 899 /* Map the shared frame, irq etc. */ 900 err = xen_blkif_map(be->blkif, ring_ref, evtchn); 901 if (err) { 902 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", 903 ring_ref, evtchn); 904 return err; 905 } 906 907 return 0; 908 } 909 910 911 /* ** Driver Registration ** */ 912 913 914 static const struct xenbus_device_id xen_blkbk_ids[] = { 915 { "vbd" }, 916 { "" } 917 }; 918 919 920 static DEFINE_XENBUS_DRIVER(xen_blkbk, , 921 .probe = xen_blkbk_probe, 922 .remove = xen_blkbk_remove, 923 .otherend_changed = frontend_changed 924 ); 925 926 927 int xen_blkif_xenbus_init(void) 928 { 929 return xenbus_register_backend(&xen_blkbk_driver); 930 } 931