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