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
9 #define pr_fmt(fmt) "xen-blkback: " fmt
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>
18 /* On the XenBus the max length of 'ring-ref%u'. */
19 #define RINGREF_NAME_LEN (20)
22 struct xenbus_device
*dev
;
23 struct xen_blkif
*blkif
;
24 struct xenbus_watch backend_watch
;
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 *,
35 static void xen_blkif_free(struct xen_blkif
*blkif
);
36 static void xen_vbd_free(struct xen_vbd
*vbd
);
38 struct xenbus_device
*xen_blkbk_xenbus(struct backend_info
*be
)
44 * The last request could free the device from softirq context and
45 * xen_blkif_free() can sleep.
47 static void xen_blkif_deferred_free(struct work_struct
*work
)
49 struct xen_blkif
*blkif
;
51 blkif
= container_of(work
, struct xen_blkif
, free_work
);
52 xen_blkif_free(blkif
);
55 static int blkback_name(struct xen_blkif
*blkif
, char *buf
)
57 char *devpath
, *devname
;
58 struct xenbus_device
*dev
= blkif
->be
->dev
;
60 devpath
= xenbus_read(XBT_NIL
, dev
->nodename
, "dev", NULL
);
62 return PTR_ERR(devpath
);
64 devname
= strstr(devpath
, "/dev/");
66 devname
+= strlen("/dev/");
70 snprintf(buf
, TASK_COMM_LEN
, "%d.%s", blkif
->domid
, devname
);
76 static void xen_update_blkif_status(struct xen_blkif
*blkif
)
79 char name
[TASK_COMM_LEN
];
80 struct xen_blkif_ring
*ring
;
83 /* Not ready to connect? */
84 if (!blkif
->rings
|| !blkif
->rings
[0].irq
|| !blkif
->vbd
.bdev_file
)
87 /* Already connected? */
88 if (blkif
->be
->dev
->state
== XenbusStateConnected
)
91 /* Attempt to connect: exit if we fail to. */
93 if (blkif
->be
->dev
->state
!= XenbusStateConnected
)
96 err
= blkback_name(blkif
, name
);
98 xenbus_dev_error(blkif
->be
->dev
, err
, "get blkback dev name");
102 err
= sync_blockdev(file_bdev(blkif
->vbd
.bdev_file
));
104 xenbus_dev_error(blkif
->be
->dev
, err
, "block flush");
107 invalidate_inode_pages2(blkif
->vbd
.bdev_file
->f_mapping
);
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
);
124 ring
= &blkif
->rings
[i
];
125 kthread_stop(ring
->xenblkd
);
130 static int xen_blkif_alloc_rings(struct xen_blkif
*blkif
)
134 blkif
->rings
= kcalloc(blkif
->nr_rings
, sizeof(struct xen_blkif_ring
),
139 for (r
= 0; r
< blkif
->nr_rings
; r
++) {
140 struct xen_blkif_ring
*ring
= &blkif
->rings
[r
];
142 spin_lock_init(&ring
->blk_ring_lock
);
143 init_waitqueue_head(&ring
->wq
);
144 INIT_LIST_HEAD(&ring
->pending_free
);
145 INIT_LIST_HEAD(&ring
->persistent_purge_list
);
146 INIT_WORK(&ring
->persistent_purge_work
, xen_blkbk_unmap_purged_grants
);
147 gnttab_page_cache_init(&ring
->free_pages
);
149 spin_lock_init(&ring
->pending_free_lock
);
150 init_waitqueue_head(&ring
->pending_free_wq
);
151 init_waitqueue_head(&ring
->shutdown_wq
);
153 ring
->st_print
= jiffies
;
160 /* Enable the persistent grants feature. */
161 static bool feature_persistent
= true;
162 module_param(feature_persistent
, bool, 0644);
163 MODULE_PARM_DESC(feature_persistent
, "Enables the persistent grants feature");
165 static struct xen_blkif
*xen_blkif_alloc(domid_t domid
)
167 struct xen_blkif
*blkif
;
169 BUILD_BUG_ON(MAX_INDIRECT_PAGES
> BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST
);
171 blkif
= kmem_cache_zalloc(xen_blkif_cachep
, GFP_KERNEL
);
173 return ERR_PTR(-ENOMEM
);
175 blkif
->domid
= domid
;
176 atomic_set(&blkif
->refcnt
, 1);
177 init_completion(&blkif
->drain_complete
);
180 * Because freeing back to the cache may be deferred, it is not
181 * safe to unload the module (and hence destroy the cache) until
182 * this has completed. To prevent premature unloading, take an
183 * extra module reference here and release only when the object
184 * has been freed back to the cache.
186 __module_get(THIS_MODULE
);
187 INIT_WORK(&blkif
->free_work
, xen_blkif_deferred_free
);
192 static int xen_blkif_map(struct xen_blkif_ring
*ring
, grant_ref_t
*gref
,
193 unsigned int nr_grefs
, unsigned int evtchn
)
196 struct xen_blkif
*blkif
= ring
->blkif
;
197 const struct blkif_common_sring
*sring_common
;
198 RING_IDX rsp_prod
, req_prod
;
201 /* Already connected through? */
205 err
= xenbus_map_ring_valloc(blkif
->be
->dev
, gref
, nr_grefs
,
210 sring_common
= (struct blkif_common_sring
*)ring
->blk_ring
;
211 rsp_prod
= READ_ONCE(sring_common
->rsp_prod
);
212 req_prod
= READ_ONCE(sring_common
->req_prod
);
214 switch (blkif
->blk_protocol
) {
215 case BLKIF_PROTOCOL_NATIVE
:
217 struct blkif_sring
*sring_native
=
218 (struct blkif_sring
*)ring
->blk_ring
;
220 BACK_RING_ATTACH(&ring
->blk_rings
.native
, sring_native
,
221 rsp_prod
, XEN_PAGE_SIZE
* nr_grefs
);
222 size
= __RING_SIZE(sring_native
, XEN_PAGE_SIZE
* nr_grefs
);
225 case BLKIF_PROTOCOL_X86_32
:
227 struct blkif_x86_32_sring
*sring_x86_32
=
228 (struct blkif_x86_32_sring
*)ring
->blk_ring
;
230 BACK_RING_ATTACH(&ring
->blk_rings
.x86_32
, sring_x86_32
,
231 rsp_prod
, XEN_PAGE_SIZE
* nr_grefs
);
232 size
= __RING_SIZE(sring_x86_32
, XEN_PAGE_SIZE
* nr_grefs
);
235 case BLKIF_PROTOCOL_X86_64
:
237 struct blkif_x86_64_sring
*sring_x86_64
=
238 (struct blkif_x86_64_sring
*)ring
->blk_ring
;
240 BACK_RING_ATTACH(&ring
->blk_rings
.x86_64
, sring_x86_64
,
241 rsp_prod
, XEN_PAGE_SIZE
* nr_grefs
);
242 size
= __RING_SIZE(sring_x86_64
, XEN_PAGE_SIZE
* nr_grefs
);
250 if (req_prod
- rsp_prod
> size
)
253 err
= bind_interdomain_evtchn_to_irqhandler_lateeoi(blkif
->be
->dev
,
254 evtchn
, xen_blkif_be_int
, 0, "blkif-backend", ring
);
262 xenbus_unmap_ring_vfree(blkif
->be
->dev
, ring
->blk_ring
);
263 ring
->blk_rings
.common
.sring
= NULL
;
267 static int xen_blkif_disconnect(struct xen_blkif
*blkif
)
269 struct pending_req
*req
, *n
;
273 for (r
= 0; r
< blkif
->nr_rings
; r
++) {
274 struct xen_blkif_ring
*ring
= &blkif
->rings
[r
];
281 kthread_stop(ring
->xenblkd
);
282 ring
->xenblkd
= NULL
;
283 wake_up(&ring
->shutdown_wq
);
286 /* The above kthread_stop() guarantees that at this point we
287 * don't have any discard_io or other_io requests. So, checking
288 * for inflight IO is enough.
290 if (atomic_read(&ring
->inflight
) > 0) {
296 unbind_from_irqhandler(ring
->irq
, ring
);
300 if (ring
->blk_rings
.common
.sring
) {
301 xenbus_unmap_ring_vfree(blkif
->be
->dev
, ring
->blk_ring
);
302 ring
->blk_rings
.common
.sring
= NULL
;
305 /* Remove all persistent grants and the cache of ballooned pages. */
306 xen_blkbk_free_caches(ring
);
308 /* Check that there is no request in use */
309 list_for_each_entry_safe(req
, n
, &ring
->pending_free
, free_list
) {
310 list_del(&req
->free_list
);
312 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++)
313 kfree(req
->segments
[j
]);
315 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++)
316 kfree(req
->indirect_pages
[j
]);
322 BUG_ON(atomic_read(&ring
->persistent_gnt_in_use
) != 0);
323 BUG_ON(!list_empty(&ring
->persistent_purge_list
));
324 BUG_ON(!RB_EMPTY_ROOT(&ring
->persistent_gnts
));
325 BUG_ON(ring
->free_pages
.num_pages
!= 0);
326 BUG_ON(ring
->persistent_gnt_c
!= 0);
327 WARN_ON(i
!= (XEN_BLKIF_REQS_PER_PAGE
* blkif
->nr_ring_pages
));
328 ring
->active
= false;
333 blkif
->nr_ring_pages
= 0;
335 * blkif->rings was allocated in connect_ring, so we should free it in
345 static void xen_blkif_free(struct xen_blkif
*blkif
)
347 WARN_ON(xen_blkif_disconnect(blkif
));
348 xen_vbd_free(&blkif
->vbd
);
349 kfree(blkif
->be
->mode
);
352 /* Make sure everything is drained before shutting down */
353 kmem_cache_free(xen_blkif_cachep
, blkif
);
354 module_put(THIS_MODULE
);
357 int __init
xen_blkif_interface_init(void)
359 xen_blkif_cachep
= kmem_cache_create("blkif_cache",
360 sizeof(struct xen_blkif
),
362 if (!xen_blkif_cachep
)
368 void xen_blkif_interface_fini(void)
370 kmem_cache_destroy(xen_blkif_cachep
);
371 xen_blkif_cachep
= NULL
;
375 * sysfs interface for VBD I/O requests
378 #define VBD_SHOW_ALLRING(name, format) \
379 static ssize_t show_##name(struct device *_dev, \
380 struct device_attribute *attr, \
383 struct xenbus_device *dev = to_xenbus_device(_dev); \
384 struct backend_info *be = dev_get_drvdata(&dev->dev); \
385 struct xen_blkif *blkif = be->blkif; \
387 unsigned long long result = 0; \
392 for (i = 0; i < blkif->nr_rings; i++) { \
393 struct xen_blkif_ring *ring = &blkif->rings[i]; \
395 result += ring->st_##name; \
399 return sprintf(buf, format, result); \
401 static DEVICE_ATTR(name, 0444, show_##name, NULL)
403 VBD_SHOW_ALLRING(oo_req
, "%llu\n");
404 VBD_SHOW_ALLRING(rd_req
, "%llu\n");
405 VBD_SHOW_ALLRING(wr_req
, "%llu\n");
406 VBD_SHOW_ALLRING(f_req
, "%llu\n");
407 VBD_SHOW_ALLRING(ds_req
, "%llu\n");
408 VBD_SHOW_ALLRING(rd_sect
, "%llu\n");
409 VBD_SHOW_ALLRING(wr_sect
, "%llu\n");
411 static struct attribute
*xen_vbdstat_attrs
[] = {
412 &dev_attr_oo_req
.attr
,
413 &dev_attr_rd_req
.attr
,
414 &dev_attr_wr_req
.attr
,
415 &dev_attr_f_req
.attr
,
416 &dev_attr_ds_req
.attr
,
417 &dev_attr_rd_sect
.attr
,
418 &dev_attr_wr_sect
.attr
,
422 static const struct attribute_group xen_vbdstat_group
= {
423 .name
= "statistics",
424 .attrs
= xen_vbdstat_attrs
,
427 #define VBD_SHOW(name, format, args...) \
428 static ssize_t show_##name(struct device *_dev, \
429 struct device_attribute *attr, \
432 struct xenbus_device *dev = to_xenbus_device(_dev); \
433 struct backend_info *be = dev_get_drvdata(&dev->dev); \
435 return sprintf(buf, format, ##args); \
437 static DEVICE_ATTR(name, 0444, show_##name, NULL)
439 VBD_SHOW(physical_device
, "%x:%x\n", be
->major
, be
->minor
);
440 VBD_SHOW(mode
, "%s\n", be
->mode
);
442 static int xenvbd_sysfs_addif(struct xenbus_device
*dev
)
446 error
= device_create_file(&dev
->dev
, &dev_attr_physical_device
);
450 error
= device_create_file(&dev
->dev
, &dev_attr_mode
);
454 error
= sysfs_create_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
460 fail3
: sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
461 fail2
: device_remove_file(&dev
->dev
, &dev_attr_mode
);
462 fail1
: device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
466 static void xenvbd_sysfs_delif(struct xenbus_device
*dev
)
468 sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
469 device_remove_file(&dev
->dev
, &dev_attr_mode
);
470 device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
473 static void xen_vbd_free(struct xen_vbd
*vbd
)
476 fput(vbd
->bdev_file
);
477 vbd
->bdev_file
= NULL
;
480 static int xen_vbd_create(struct xen_blkif
*blkif
, blkif_vdev_t handle
,
481 unsigned major
, unsigned minor
, int readonly
,
485 struct file
*bdev_file
;
488 vbd
->handle
= handle
;
489 vbd
->readonly
= readonly
;
492 vbd
->pdevice
= MKDEV(major
, minor
);
494 bdev_file
= bdev_file_open_by_dev(vbd
->pdevice
, vbd
->readonly
?
495 BLK_OPEN_READ
: BLK_OPEN_WRITE
, NULL
, NULL
);
497 if (IS_ERR(bdev_file
)) {
498 pr_warn("xen_vbd_create: device %08x could not be opened\n",
503 vbd
->bdev_file
= bdev_file
;
504 if (file_bdev(vbd
->bdev_file
)->bd_disk
== NULL
) {
505 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
510 vbd
->size
= vbd_sz(vbd
);
512 if (cdrom
|| disk_to_cdi(file_bdev(vbd
->bdev_file
)->bd_disk
))
513 vbd
->type
|= VDISK_CDROM
;
514 if (file_bdev(vbd
->bdev_file
)->bd_disk
->flags
& GENHD_FL_REMOVABLE
)
515 vbd
->type
|= VDISK_REMOVABLE
;
517 if (bdev_write_cache(file_bdev(bdev_file
)))
518 vbd
->flush_support
= true;
519 if (bdev_max_secure_erase_sectors(file_bdev(bdev_file
)))
520 vbd
->discard_secure
= true;
522 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
523 handle
, blkif
->domid
);
527 static void xen_blkbk_remove(struct xenbus_device
*dev
)
529 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
531 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
533 if (be
->major
|| be
->minor
)
534 xenvbd_sysfs_delif(dev
);
536 if (be
->backend_watch
.node
) {
537 unregister_xenbus_watch(&be
->backend_watch
);
538 kfree(be
->backend_watch
.node
);
539 be
->backend_watch
.node
= NULL
;
542 dev_set_drvdata(&dev
->dev
, NULL
);
545 xen_blkif_disconnect(be
->blkif
);
547 /* Put the reference we set in xen_blkif_alloc(). */
548 xen_blkif_put(be
->blkif
);
552 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
553 struct backend_info
*be
, int state
)
555 struct xenbus_device
*dev
= be
->dev
;
558 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-flush-cache",
561 dev_warn(&dev
->dev
, "writing feature-flush-cache (%d)", err
);
566 static void xen_blkbk_discard(struct xenbus_transaction xbt
, struct backend_info
*be
)
568 struct xenbus_device
*dev
= be
->dev
;
569 struct xen_blkif
*blkif
= be
->blkif
;
572 struct block_device
*bdev
= file_bdev(be
->blkif
->vbd
.bdev_file
);
574 if (!xenbus_read_unsigned(dev
->nodename
, "discard-enable", 1))
577 if (bdev_max_discard_sectors(bdev
)) {
578 err
= xenbus_printf(xbt
, dev
->nodename
,
579 "discard-granularity", "%u",
580 bdev_discard_granularity(bdev
));
582 dev_warn(&dev
->dev
, "writing discard-granularity (%d)", err
);
585 err
= xenbus_printf(xbt
, dev
->nodename
,
586 "discard-alignment", "%u",
587 bdev_discard_alignment(bdev
));
589 dev_warn(&dev
->dev
, "writing discard-alignment (%d)", err
);
594 err
= xenbus_printf(xbt
, dev
->nodename
,
595 "discard-secure", "%d",
596 blkif
->vbd
.discard_secure
);
598 dev_warn(&dev
->dev
, "writing discard-secure (%d)", err
);
602 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-discard",
605 dev_warn(&dev
->dev
, "writing feature-discard (%d)", err
);
608 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
609 struct backend_info
*be
, int state
)
611 struct xenbus_device
*dev
= be
->dev
;
614 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-barrier",
617 dev_warn(&dev
->dev
, "writing feature-barrier (%d)", err
);
623 * Entry point to this code when a new device is created. Allocate the basic
624 * structures, and watch the store waiting for the hotplug scripts to tell us
625 * the device's physical major and minor numbers. Switch to InitWait.
627 static int xen_blkbk_probe(struct xenbus_device
*dev
,
628 const struct xenbus_device_id
*id
)
631 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
634 /* match the pr_debug in xen_blkbk_remove */
635 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
638 xenbus_dev_fatal(dev
, -ENOMEM
,
639 "allocating backend structure");
643 dev_set_drvdata(&dev
->dev
, be
);
645 be
->blkif
= xen_blkif_alloc(dev
->otherend_id
);
646 if (IS_ERR(be
->blkif
)) {
647 err
= PTR_ERR(be
->blkif
);
649 xenbus_dev_fatal(dev
, err
, "creating block interface");
653 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
654 "feature-max-indirect-segments", "%u",
655 MAX_INDIRECT_SEGMENTS
);
658 "writing %s/feature-max-indirect-segments (%d)",
661 /* Multi-queue: advertise how many queues are supported by us.*/
662 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
663 "multi-queue-max-queues", "%u", xenblk_max_queues
);
665 pr_warn("Error writing multi-queue-max-queues\n");
667 /* setup back pointer */
670 err
= xenbus_watch_pathfmt(dev
, &be
->backend_watch
, NULL
,
672 "%s/%s", dev
->nodename
, "physical-device");
676 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "max-ring-page-order", "%u",
677 xen_blkif_max_ring_order
);
679 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__
);
681 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
688 pr_warn("%s failed\n", __func__
);
689 xen_blkbk_remove(dev
);
694 * Callback received when the hotplug scripts have placed the physical-device
695 * node. Read it and the mode node, and create a vbd. If the frontend is
698 static void backend_changed(struct xenbus_watch
*watch
,
699 const char *path
, const char *token
)
704 struct backend_info
*be
705 = container_of(watch
, struct backend_info
, backend_watch
);
706 struct xenbus_device
*dev
= be
->dev
;
708 unsigned long handle
;
711 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
713 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "physical-device", "%x:%x",
715 if (XENBUS_EXIST_ERR(err
)) {
717 * Since this watch will fire once immediately after it is
718 * registered, we expect this. Ignore it, and wait for the
724 xenbus_dev_fatal(dev
, err
, "reading physical-device");
728 if (be
->major
| be
->minor
) {
729 if (be
->major
!= major
|| be
->minor
!= minor
)
730 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
731 be
->major
, be
->minor
, major
, minor
);
735 be
->mode
= xenbus_read(XBT_NIL
, dev
->nodename
, "mode", NULL
);
736 if (IS_ERR(be
->mode
)) {
737 err
= PTR_ERR(be
->mode
);
739 xenbus_dev_fatal(dev
, err
, "reading mode");
743 device_type
= xenbus_read(XBT_NIL
, dev
->otherend
, "device-type", NULL
);
744 if (!IS_ERR(device_type
)) {
745 cdrom
= strcmp(device_type
, "cdrom") == 0;
749 /* Front end dir is a number, which is used as the handle. */
750 err
= kstrtoul(strrchr(dev
->otherend
, '/') + 1, 0, &handle
);
760 err
= xen_vbd_create(be
->blkif
, handle
, major
, minor
,
761 !strchr(be
->mode
, 'w'), cdrom
);
764 xenbus_dev_fatal(dev
, err
, "creating vbd structure");
766 err
= xenvbd_sysfs_addif(dev
);
768 xen_vbd_free(&be
->blkif
->vbd
);
769 xenbus_dev_fatal(dev
, err
, "creating sysfs entries");
779 /* We're potentially connected now */
780 xen_update_blkif_status(be
->blkif
);
785 * Callback received when the frontend's state changes.
787 static void frontend_changed(struct xenbus_device
*dev
,
788 enum xenbus_state frontend_state
)
790 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
793 pr_debug("%s %p %s\n", __func__
, dev
, xenbus_strstate(frontend_state
));
795 switch (frontend_state
) {
796 case XenbusStateInitialising
:
797 if (dev
->state
== XenbusStateClosed
) {
798 pr_info("%s: prepare for reconnect\n", dev
->nodename
);
799 xenbus_switch_state(dev
, XenbusStateInitWait
);
803 case XenbusStateInitialised
:
804 case XenbusStateConnected
:
806 * Ensure we connect even when two watches fire in
807 * close succession and we miss the intermediate value
810 if (dev
->state
== XenbusStateConnected
)
814 * Enforce precondition before potential leak point.
815 * xen_blkif_disconnect() is idempotent.
817 err
= xen_blkif_disconnect(be
->blkif
);
819 xenbus_dev_fatal(dev
, err
, "pending I/O");
823 err
= connect_ring(be
);
826 * Clean up so that memory resources can be used by
827 * other devices. connect_ring reported already error.
829 xen_blkif_disconnect(be
->blkif
);
832 xen_update_blkif_status(be
->blkif
);
835 case XenbusStateClosing
:
836 xenbus_switch_state(dev
, XenbusStateClosing
);
839 case XenbusStateClosed
:
840 xen_blkif_disconnect(be
->blkif
);
841 xenbus_switch_state(dev
, XenbusStateClosed
);
842 if (xenbus_dev_is_online(dev
))
846 case XenbusStateUnknown
:
847 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
848 device_unregister(&dev
->dev
);
852 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
858 /* Once a memory pressure is detected, squeeze free page pools for a while. */
859 static unsigned int buffer_squeeze_duration_ms
= 10;
860 module_param_named(buffer_squeeze_duration_ms
,
861 buffer_squeeze_duration_ms
, int, 0644);
862 MODULE_PARM_DESC(buffer_squeeze_duration_ms
,
863 "Duration in ms to squeeze pages buffer when a memory pressure is detected");
866 * Callback received when the memory pressure is detected.
868 static void reclaim_memory(struct xenbus_device
*dev
)
870 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
874 be
->blkif
->buffer_squeeze_end
= jiffies
+
875 msecs_to_jiffies(buffer_squeeze_duration_ms
);
878 /* ** Connection ** */
881 * Write the physical details regarding the block device to the store, and
882 * switch to Connected state.
884 static void connect(struct backend_info
*be
)
886 struct xenbus_transaction xbt
;
888 struct xenbus_device
*dev
= be
->dev
;
890 pr_debug("%s %s\n", __func__
, dev
->otherend
);
892 /* Supply the information about the device the frontend needs */
894 err
= xenbus_transaction_start(&xbt
);
896 xenbus_dev_fatal(dev
, err
, "starting transaction");
900 /* If we can't advertise it is OK. */
901 xen_blkbk_flush_diskcache(xbt
, be
, be
->blkif
->vbd
.flush_support
);
903 xen_blkbk_discard(xbt
, be
);
905 xen_blkbk_barrier(xbt
, be
, be
->blkif
->vbd
.flush_support
);
907 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-persistent", "%u",
908 be
->blkif
->vbd
.feature_gnt_persistent_parm
);
910 xenbus_dev_fatal(dev
, err
, "writing %s/feature-persistent",
915 err
= xenbus_printf(xbt
, dev
->nodename
, "sectors", "%llu",
916 (unsigned long long)vbd_sz(&be
->blkif
->vbd
));
918 xenbus_dev_fatal(dev
, err
, "writing %s/sectors",
923 /* FIXME: use a typename instead */
924 err
= xenbus_printf(xbt
, dev
->nodename
, "info", "%u",
925 be
->blkif
->vbd
.type
|
926 (be
->blkif
->vbd
.readonly
? VDISK_READONLY
: 0));
928 xenbus_dev_fatal(dev
, err
, "writing %s/info",
932 err
= xenbus_printf(xbt
, dev
->nodename
, "sector-size", "%lu",
933 (unsigned long)bdev_logical_block_size(
934 file_bdev(be
->blkif
->vbd
.bdev_file
)));
936 xenbus_dev_fatal(dev
, err
, "writing %s/sector-size",
940 err
= xenbus_printf(xbt
, dev
->nodename
, "physical-sector-size", "%u",
941 bdev_physical_block_size(
942 file_bdev(be
->blkif
->vbd
.bdev_file
)));
944 xenbus_dev_error(dev
, err
, "writing %s/physical-sector-size",
947 err
= xenbus_transaction_end(xbt
, 0);
951 xenbus_dev_fatal(dev
, err
, "ending transaction");
953 err
= xenbus_switch_state(dev
, XenbusStateConnected
);
955 xenbus_dev_fatal(dev
, err
, "%s: switching to Connected state",
960 xenbus_transaction_end(xbt
, 1);
964 * Each ring may have multi pages, depends on "ring-page-order".
966 static int read_per_ring_refs(struct xen_blkif_ring
*ring
, const char *dir
)
968 unsigned int ring_ref
[XENBUS_MAX_RING_GRANTS
];
969 struct pending_req
*req
, *n
;
971 struct xen_blkif
*blkif
= ring
->blkif
;
972 struct xenbus_device
*dev
= blkif
->be
->dev
;
973 unsigned int nr_grefs
, evtchn
;
975 err
= xenbus_scanf(XBT_NIL
, dir
, "event-channel", "%u",
979 xenbus_dev_fatal(dev
, err
, "reading %s/event-channel", dir
);
983 nr_grefs
= blkif
->nr_ring_pages
;
985 if (unlikely(!nr_grefs
)) {
990 for (i
= 0; i
< nr_grefs
; i
++) {
991 char ring_ref_name
[RINGREF_NAME_LEN
];
993 if (blkif
->multi_ref
)
994 snprintf(ring_ref_name
, RINGREF_NAME_LEN
, "ring-ref%u", i
);
997 snprintf(ring_ref_name
, RINGREF_NAME_LEN
, "ring-ref");
1000 err
= xenbus_scanf(XBT_NIL
, dir
, ring_ref_name
,
1001 "%u", &ring_ref
[i
]);
1005 xenbus_dev_fatal(dev
, err
, "reading %s/%s",
1006 dir
, ring_ref_name
);
1012 for (i
= 0; i
< nr_grefs
* XEN_BLKIF_REQS_PER_PAGE
; i
++) {
1013 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
1016 list_add_tail(&req
->free_list
, &ring
->pending_free
);
1017 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
1018 req
->segments
[j
] = kzalloc(sizeof(*req
->segments
[0]), GFP_KERNEL
);
1019 if (!req
->segments
[j
])
1022 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
1023 req
->indirect_pages
[j
] = kzalloc(sizeof(*req
->indirect_pages
[0]),
1025 if (!req
->indirect_pages
[j
])
1030 /* Map the shared frame, irq etc. */
1031 err
= xen_blkif_map(ring
, ring_ref
, nr_grefs
, evtchn
);
1033 xenbus_dev_fatal(dev
, err
, "mapping ring-ref port %u", evtchn
);
1040 list_for_each_entry_safe(req
, n
, &ring
->pending_free
, free_list
) {
1041 list_del(&req
->free_list
);
1042 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
1043 if (!req
->segments
[j
])
1045 kfree(req
->segments
[j
]);
1047 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
1048 if (!req
->indirect_pages
[j
])
1050 kfree(req
->indirect_pages
[j
]);
1057 static int connect_ring(struct backend_info
*be
)
1059 struct xenbus_device
*dev
= be
->dev
;
1060 struct xen_blkif
*blkif
= be
->blkif
;
1061 char protocol
[64] = "";
1065 const size_t xenstore_path_ext_size
= 11; /* sufficient for "/queue-NNN" */
1066 unsigned int requested_num_queues
= 0;
1067 unsigned int ring_page_order
;
1069 pr_debug("%s %s\n", __func__
, dev
->otherend
);
1071 blkif
->blk_protocol
= BLKIF_PROTOCOL_DEFAULT
;
1072 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "protocol",
1075 strcpy(protocol
, "unspecified, assuming default");
1076 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_NATIVE
))
1077 blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
1078 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_32
))
1079 blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_32
;
1080 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_64
))
1081 blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_64
;
1083 xenbus_dev_fatal(dev
, err
, "unknown fe protocol %s", protocol
);
1087 blkif
->vbd
.feature_gnt_persistent_parm
= feature_persistent
;
1088 blkif
->vbd
.feature_gnt_persistent
=
1089 blkif
->vbd
.feature_gnt_persistent_parm
&&
1090 xenbus_read_unsigned(dev
->otherend
, "feature-persistent", 0);
1092 blkif
->vbd
.overflow_max_grants
= 0;
1095 * Read the number of hardware queues from frontend.
1097 requested_num_queues
= xenbus_read_unsigned(dev
->otherend
,
1098 "multi-queue-num-queues",
1100 if (requested_num_queues
> xenblk_max_queues
1101 || requested_num_queues
== 0) {
1102 /* Buggy or malicious guest. */
1103 xenbus_dev_fatal(dev
, err
,
1104 "guest requested %u queues, exceeding the maximum of %u.",
1105 requested_num_queues
, xenblk_max_queues
);
1108 blkif
->nr_rings
= requested_num_queues
;
1109 if (xen_blkif_alloc_rings(blkif
))
1112 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev
->nodename
,
1113 blkif
->nr_rings
, blkif
->blk_protocol
, protocol
,
1114 blkif
->vbd
.feature_gnt_persistent
? "persistent grants" : "");
1116 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "ring-page-order", "%u",
1119 blkif
->nr_ring_pages
= 1;
1120 blkif
->multi_ref
= false;
1121 } else if (ring_page_order
<= xen_blkif_max_ring_order
) {
1122 blkif
->nr_ring_pages
= 1 << ring_page_order
;
1123 blkif
->multi_ref
= true;
1126 xenbus_dev_fatal(dev
, err
,
1127 "requested ring page order %d exceed max:%d",
1129 xen_blkif_max_ring_order
);
1133 if (blkif
->nr_rings
== 1)
1134 return read_per_ring_refs(&blkif
->rings
[0], dev
->otherend
);
1136 xspathsize
= strlen(dev
->otherend
) + xenstore_path_ext_size
;
1137 xspath
= kmalloc(xspathsize
, GFP_KERNEL
);
1139 xenbus_dev_fatal(dev
, -ENOMEM
, "reading ring references");
1143 for (i
= 0; i
< blkif
->nr_rings
; i
++) {
1144 memset(xspath
, 0, xspathsize
);
1145 snprintf(xspath
, xspathsize
, "%s/queue-%u", dev
->otherend
, i
);
1146 err
= read_per_ring_refs(&blkif
->rings
[i
], xspath
);
1157 static const struct xenbus_device_id xen_blkbk_ids
[] = {
1162 static struct xenbus_driver xen_blkbk_driver
= {
1163 .ids
= xen_blkbk_ids
,
1164 .probe
= xen_blkbk_probe
,
1165 .remove
= xen_blkbk_remove
,
1166 .otherend_changed
= frontend_changed
,
1167 .allow_rebind
= true,
1168 .reclaim_memory
= reclaim_memory
,
1171 int xen_blkif_xenbus_init(void)
1173 return xenbus_register_backend(&xen_blkbk_driver
);
1176 void xen_blkif_xenbus_fini(void)
1178 xenbus_unregister_driver(&xen_blkbk_driver
);