Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [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 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kthread.h> |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 20 | #include <xen/events.h> |
| 21 | #include <xen/grant_table.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 22 | #include "common.h" |
| 23 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 24 | struct backend_info { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 25 | struct xenbus_device *dev; |
Konrad Rzeszutek Wilk | 5185432 | 2011-05-12 18:02:28 -0400 | [diff] [blame] | 26 | struct xen_blkif *blkif; |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 27 | struct xenbus_watch backend_watch; |
| 28 | unsigned major; |
| 29 | unsigned minor; |
| 30 | char *mode; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 31 | }; |
| 32 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 33 | static struct kmem_cache *xen_blkif_cachep; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 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 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 39 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) |
Jeremy Fitzhardinge | 98e036a | 2010-03-18 15:35:05 -0700 | [diff] [blame] | 40 | { |
| 41 | return be->dev; |
| 42 | } |
| 43 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 44 | static int blkback_name(struct xen_blkif *blkif, char *buf) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 45 | { |
| 46 | char *devpath, *devname; |
| 47 | struct xenbus_device *dev = blkif->be->dev; |
| 48 | |
| 49 | devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); |
| 50 | if (IS_ERR(devpath)) |
| 51 | return PTR_ERR(devpath); |
| 52 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 53 | devname = strstr(devpath, "/dev/"); |
| 54 | if (devname != NULL) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 55 | devname += strlen("/dev/"); |
| 56 | else |
| 57 | devname = devpath; |
| 58 | |
| 59 | snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname); |
| 60 | kfree(devpath); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 65 | static void xen_update_blkif_status(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 66 | { |
| 67 | int err; |
| 68 | char name[TASK_COMM_LEN]; |
| 69 | |
| 70 | /* Not ready to connect? */ |
| 71 | if (!blkif->irq || !blkif->vbd.bdev) |
| 72 | return; |
| 73 | |
| 74 | /* Already connected? */ |
| 75 | if (blkif->be->dev->state == XenbusStateConnected) |
| 76 | return; |
| 77 | |
| 78 | /* Attempt to connect: exit if we fail to. */ |
| 79 | connect(blkif->be); |
| 80 | if (blkif->be->dev->state != XenbusStateConnected) |
| 81 | return; |
| 82 | |
| 83 | err = blkback_name(blkif, name); |
| 84 | if (err) { |
| 85 | xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); |
| 86 | return; |
| 87 | } |
| 88 | |
Chris Lalancette | cbf4629 | 2010-07-21 12:41:45 -0700 | [diff] [blame] | 89 | err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); |
| 90 | if (err) { |
| 91 | xenbus_dev_error(blkif->be->dev, err, "block flush"); |
| 92 | return; |
| 93 | } |
| 94 | invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); |
| 95 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 96 | blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 97 | if (IS_ERR(blkif->xenblkd)) { |
| 98 | err = PTR_ERR(blkif->xenblkd); |
| 99 | blkif->xenblkd = NULL; |
| 100 | xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); |
| 101 | } |
| 102 | } |
| 103 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 104 | static struct xen_blkif *xen_blkif_alloc(domid_t domid) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 105 | { |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 106 | struct xen_blkif *blkif; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 107 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 108 | blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 109 | if (!blkif) |
| 110 | return ERR_PTR(-ENOMEM); |
| 111 | |
| 112 | memset(blkif, 0, sizeof(*blkif)); |
| 113 | blkif->domid = domid; |
| 114 | spin_lock_init(&blkif->blk_ring_lock); |
| 115 | atomic_set(&blkif->refcnt, 1); |
| 116 | init_waitqueue_head(&blkif->wq); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 117 | init_completion(&blkif->drain_complete); |
| 118 | atomic_set(&blkif->drain, 0); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 119 | blkif->st_print = jiffies; |
| 120 | init_waitqueue_head(&blkif->waiting_to_free); |
| 121 | |
| 122 | return blkif; |
| 123 | } |
| 124 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 125 | static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 126 | unsigned int evtchn) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 127 | { |
| 128 | int err; |
| 129 | |
| 130 | /* Already connected through? */ |
| 131 | if (blkif->irq) |
| 132 | return 0; |
| 133 | |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 134 | err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring); |
| 135 | if (err < 0) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 136 | return err; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 137 | |
| 138 | switch (blkif->blk_protocol) { |
| 139 | case BLKIF_PROTOCOL_NATIVE: |
| 140 | { |
| 141 | struct blkif_sring *sring; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 142 | sring = (struct blkif_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 143 | BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); |
| 144 | break; |
| 145 | } |
| 146 | case BLKIF_PROTOCOL_X86_32: |
| 147 | { |
| 148 | struct blkif_x86_32_sring *sring_x86_32; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 149 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 150 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); |
| 151 | break; |
| 152 | } |
| 153 | case BLKIF_PROTOCOL_X86_64: |
| 154 | { |
| 155 | struct blkif_x86_64_sring *sring_x86_64; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 156 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 157 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | BUG(); |
| 162 | } |
| 163 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 164 | err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, |
| 165 | xen_blkif_be_int, 0, |
| 166 | "blkif-backend", blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 167 | if (err < 0) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 168 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 169 | blkif->blk_rings.common.sring = NULL; |
| 170 | return err; |
| 171 | } |
| 172 | blkif->irq = err; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 177 | static void xen_blkif_disconnect(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 178 | { |
| 179 | if (blkif->xenblkd) { |
| 180 | kthread_stop(blkif->xenblkd); |
| 181 | blkif->xenblkd = NULL; |
| 182 | } |
| 183 | |
| 184 | atomic_dec(&blkif->refcnt); |
| 185 | wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0); |
| 186 | atomic_inc(&blkif->refcnt); |
| 187 | |
| 188 | if (blkif->irq) { |
| 189 | unbind_from_irqhandler(blkif->irq, blkif); |
| 190 | blkif->irq = 0; |
| 191 | } |
| 192 | |
| 193 | if (blkif->blk_rings.common.sring) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 194 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 195 | blkif->blk_rings.common.sring = NULL; |
| 196 | } |
| 197 | } |
| 198 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 199 | void xen_blkif_free(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 200 | { |
| 201 | if (!atomic_dec_and_test(&blkif->refcnt)) |
| 202 | BUG(); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 203 | kmem_cache_free(xen_blkif_cachep, blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 206 | int __init xen_blkif_interface_init(void) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 207 | { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 208 | xen_blkif_cachep = kmem_cache_create("blkif_cache", |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 209 | sizeof(struct xen_blkif), |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 210 | 0, 0, NULL); |
| 211 | if (!xen_blkif_cachep) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 212 | return -ENOMEM; |
| 213 | |
| 214 | return 0; |
| 215 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 216 | |
Konrad Rzeszutek Wilk | a1397fa | 2011-04-14 17:05:23 -0400 | [diff] [blame] | 217 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 218 | * sysfs interface for VBD I/O requests |
| 219 | */ |
| 220 | |
| 221 | #define VBD_SHOW(name, format, args...) \ |
| 222 | static ssize_t show_##name(struct device *_dev, \ |
| 223 | struct device_attribute *attr, \ |
| 224 | char *buf) \ |
| 225 | { \ |
| 226 | struct xenbus_device *dev = to_xenbus_device(_dev); \ |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 227 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 228 | \ |
| 229 | return sprintf(buf, format, ##args); \ |
| 230 | } \ |
| 231 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 232 | |
| 233 | VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req); |
| 234 | VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req); |
| 235 | VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req); |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 236 | VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 237 | VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 238 | VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect); |
| 239 | VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect); |
| 240 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 241 | static struct attribute *xen_vbdstat_attrs[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 242 | &dev_attr_oo_req.attr, |
| 243 | &dev_attr_rd_req.attr, |
| 244 | &dev_attr_wr_req.attr, |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 245 | &dev_attr_f_req.attr, |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 246 | &dev_attr_ds_req.attr, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 247 | &dev_attr_rd_sect.attr, |
| 248 | &dev_attr_wr_sect.attr, |
| 249 | NULL |
| 250 | }; |
| 251 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 252 | static struct attribute_group xen_vbdstat_group = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 253 | .name = "statistics", |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 254 | .attrs = xen_vbdstat_attrs, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); |
| 258 | VBD_SHOW(mode, "%s\n", be->mode); |
| 259 | |
| 260 | int xenvbd_sysfs_addif(struct xenbus_device *dev) |
| 261 | { |
| 262 | int error; |
| 263 | |
| 264 | error = device_create_file(&dev->dev, &dev_attr_physical_device); |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 265 | if (error) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 266 | goto fail1; |
| 267 | |
| 268 | error = device_create_file(&dev->dev, &dev_attr_mode); |
| 269 | if (error) |
| 270 | goto fail2; |
| 271 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 272 | error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 273 | if (error) |
| 274 | goto fail3; |
| 275 | |
| 276 | return 0; |
| 277 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 278 | fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 279 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); |
| 280 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 281 | return error; |
| 282 | } |
| 283 | |
| 284 | void xenvbd_sysfs_delif(struct xenbus_device *dev) |
| 285 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 286 | sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 287 | device_remove_file(&dev->dev, &dev_attr_mode); |
| 288 | device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 289 | } |
| 290 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 291 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 292 | static void xen_vbd_free(struct xen_vbd *vbd) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 293 | { |
| 294 | if (vbd->bdev) |
| 295 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); |
| 296 | vbd->bdev = NULL; |
| 297 | } |
| 298 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 299 | static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, |
| 300 | unsigned major, unsigned minor, int readonly, |
| 301 | int cdrom) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 302 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 303 | struct xen_vbd *vbd; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 304 | struct block_device *bdev; |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 305 | struct request_queue *q; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 306 | |
| 307 | vbd = &blkif->vbd; |
| 308 | vbd->handle = handle; |
| 309 | vbd->readonly = readonly; |
| 310 | vbd->type = 0; |
| 311 | |
| 312 | vbd->pdevice = MKDEV(major, minor); |
| 313 | |
| 314 | bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? |
| 315 | FMODE_READ : FMODE_WRITE, NULL); |
| 316 | |
| 317 | if (IS_ERR(bdev)) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 318 | DPRINTK("xen_vbd_create: device %08x could not be opened.\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 319 | vbd->pdevice); |
| 320 | return -ENOENT; |
| 321 | } |
| 322 | |
| 323 | vbd->bdev = bdev; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 324 | if (vbd->bdev->bd_disk == NULL) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 325 | DPRINTK("xen_vbd_create: device %08x doesn't exist.\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 326 | vbd->pdevice); |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 327 | xen_vbd_free(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 328 | return -ENOENT; |
| 329 | } |
Laszlo Ersek | 6464920 | 2011-05-25 12:24:25 +0200 | [diff] [blame] | 330 | vbd->size = vbd_sz(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 331 | |
| 332 | if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) |
| 333 | vbd->type |= VDISK_CDROM; |
| 334 | if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) |
| 335 | vbd->type |= VDISK_REMOVABLE; |
| 336 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 337 | q = bdev_get_queue(bdev); |
| 338 | if (q && q->flush_flags) |
| 339 | vbd->flush_support = true; |
| 340 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 341 | if (q && blk_queue_secdiscard(q)) |
| 342 | vbd->discard_secure = true; |
| 343 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 344 | DPRINTK("Successful creation of handle=%04x (dom=%u)\n", |
| 345 | handle, blkif->domid); |
| 346 | return 0; |
| 347 | } |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 348 | static int xen_blkbk_remove(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 349 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 350 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 351 | |
| 352 | DPRINTK(""); |
| 353 | |
| 354 | if (be->major || be->minor) |
| 355 | xenvbd_sysfs_delif(dev); |
| 356 | |
| 357 | if (be->backend_watch.node) { |
| 358 | unregister_xenbus_watch(&be->backend_watch); |
| 359 | kfree(be->backend_watch.node); |
| 360 | be->backend_watch.node = NULL; |
| 361 | } |
| 362 | |
| 363 | if (be->blkif) { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 364 | xen_blkif_disconnect(be->blkif); |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 365 | xen_vbd_free(&be->blkif->vbd); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 366 | xen_blkif_free(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 367 | be->blkif = NULL; |
| 368 | } |
| 369 | |
| 370 | kfree(be); |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 371 | dev_set_drvdata(&dev->dev, NULL); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 375 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
| 376 | struct backend_info *be, int state) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 377 | { |
| 378 | struct xenbus_device *dev = be->dev; |
| 379 | int err; |
| 380 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 381 | err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 382 | "%d", state); |
| 383 | if (err) |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 384 | xenbus_dev_fatal(dev, err, "writing feature-flush-cache"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 385 | |
| 386 | return err; |
| 387 | } |
| 388 | |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 389 | int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) |
| 390 | { |
| 391 | struct xenbus_device *dev = be->dev; |
| 392 | struct xen_blkif *blkif = be->blkif; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 393 | int err; |
| 394 | int state = 0; |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame^] | 395 | struct block_device *bdev = be->blkif->vbd.bdev; |
| 396 | struct request_queue *q = bdev_get_queue(bdev); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 397 | |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame^] | 398 | if (blk_queue_discard(q)) { |
| 399 | err = xenbus_printf(xbt, dev->nodename, |
| 400 | "discard-granularity", "%u", |
| 401 | q->limits.discard_granularity); |
| 402 | if (err) { |
| 403 | xenbus_dev_fatal(dev, err, |
| 404 | "writing discard-granularity"); |
| 405 | goto out; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 406 | } |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame^] | 407 | err = xenbus_printf(xbt, dev->nodename, |
| 408 | "discard-alignment", "%u", |
| 409 | q->limits.discard_alignment); |
| 410 | if (err) { |
| 411 | xenbus_dev_fatal(dev, err, |
| 412 | "writing discard-alignment"); |
| 413 | goto out; |
| 414 | } |
| 415 | state = 1; |
| 416 | /* Optional. */ |
| 417 | err = xenbus_printf(xbt, dev->nodename, |
| 418 | "discard-secure", "%d", |
| 419 | blkif->vbd.discard_secure); |
| 420 | if (err) { |
| 421 | xenbus_dev_fatal(dev, err, |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 422 | "writting discard-secure"); |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame^] | 423 | goto out; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 424 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 425 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 426 | err = xenbus_printf(xbt, dev->nodename, "feature-discard", |
| 427 | "%d", state); |
| 428 | if (err) |
| 429 | xenbus_dev_fatal(dev, err, "writing feature-discard"); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 430 | out: |
| 431 | return err; |
| 432 | } |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 433 | int xen_blkbk_barrier(struct xenbus_transaction xbt, |
| 434 | struct backend_info *be, int state) |
| 435 | { |
| 436 | struct xenbus_device *dev = be->dev; |
| 437 | int err; |
| 438 | |
| 439 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", |
| 440 | "%d", state); |
| 441 | if (err) |
| 442 | xenbus_dev_fatal(dev, err, "writing feature-barrier"); |
| 443 | |
| 444 | return err; |
| 445 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 446 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 447 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 448 | * Entry point to this code when a new device is created. Allocate the basic |
| 449 | * structures, and watch the store waiting for the hotplug scripts to tell us |
| 450 | * the device's physical major and minor numbers. Switch to InitWait. |
| 451 | */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 452 | static int xen_blkbk_probe(struct xenbus_device *dev, |
| 453 | const struct xenbus_device_id *id) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 454 | { |
| 455 | int err; |
| 456 | struct backend_info *be = kzalloc(sizeof(struct backend_info), |
| 457 | GFP_KERNEL); |
| 458 | if (!be) { |
| 459 | xenbus_dev_fatal(dev, -ENOMEM, |
| 460 | "allocating backend structure"); |
| 461 | return -ENOMEM; |
| 462 | } |
| 463 | be->dev = dev; |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 464 | dev_set_drvdata(&dev->dev, be); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 465 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 466 | be->blkif = xen_blkif_alloc(dev->otherend_id); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 467 | if (IS_ERR(be->blkif)) { |
| 468 | err = PTR_ERR(be->blkif); |
| 469 | be->blkif = NULL; |
| 470 | xenbus_dev_fatal(dev, err, "creating block interface"); |
| 471 | goto fail; |
| 472 | } |
| 473 | |
| 474 | /* setup back pointer */ |
| 475 | be->blkif->be = be; |
| 476 | |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 477 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
| 478 | "%s/%s", dev->nodename, "physical-device"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 479 | if (err) |
| 480 | goto fail; |
| 481 | |
| 482 | err = xenbus_switch_state(dev, XenbusStateInitWait); |
| 483 | if (err) |
| 484 | goto fail; |
| 485 | |
| 486 | return 0; |
| 487 | |
| 488 | fail: |
| 489 | DPRINTK("failed"); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 490 | xen_blkbk_remove(dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 491 | return err; |
| 492 | } |
| 493 | |
| 494 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 495 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 496 | * Callback received when the hotplug scripts have placed the physical-device |
| 497 | * node. Read it and the mode node, and create a vbd. If the frontend is |
| 498 | * ready, connect. |
| 499 | */ |
| 500 | static void backend_changed(struct xenbus_watch *watch, |
| 501 | const char **vec, unsigned int len) |
| 502 | { |
| 503 | int err; |
| 504 | unsigned major; |
| 505 | unsigned minor; |
| 506 | struct backend_info *be |
| 507 | = container_of(watch, struct backend_info, backend_watch); |
| 508 | struct xenbus_device *dev = be->dev; |
| 509 | int cdrom = 0; |
| 510 | char *device_type; |
| 511 | |
| 512 | DPRINTK(""); |
| 513 | |
| 514 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", |
| 515 | &major, &minor); |
| 516 | if (XENBUS_EXIST_ERR(err)) { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 517 | /* |
| 518 | * Since this watch will fire once immediately after it is |
| 519 | * registered, we expect this. Ignore it, and wait for the |
| 520 | * hotplug scripts. |
| 521 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | if (err != 2) { |
| 525 | xenbus_dev_fatal(dev, err, "reading physical-device"); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if ((be->major || be->minor) && |
| 530 | ((be->major != major) || (be->minor != minor))) { |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 531 | pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n", |
Konrad Rzeszutek Wilk | ebe8190 | 2011-05-12 16:42:31 -0400 | [diff] [blame] | 532 | be->major, be->minor, major, minor); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
| 536 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); |
| 537 | if (IS_ERR(be->mode)) { |
| 538 | err = PTR_ERR(be->mode); |
| 539 | be->mode = NULL; |
| 540 | xenbus_dev_fatal(dev, err, "reading mode"); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); |
| 545 | if (!IS_ERR(device_type)) { |
| 546 | cdrom = strcmp(device_type, "cdrom") == 0; |
| 547 | kfree(device_type); |
| 548 | } |
| 549 | |
| 550 | if (be->major == 0 && be->minor == 0) { |
| 551 | /* Front end dir is a number, which is used as the handle. */ |
| 552 | |
| 553 | char *p = strrchr(dev->otherend, '/') + 1; |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 554 | long handle; |
| 555 | err = strict_strtoul(p, 0, &handle); |
| 556 | if (err) |
| 557 | return; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 558 | |
| 559 | be->major = major; |
| 560 | be->minor = minor; |
| 561 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 562 | err = xen_vbd_create(be->blkif, handle, major, minor, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 563 | (NULL == strchr(be->mode, 'w')), cdrom); |
| 564 | if (err) { |
Konrad Rzeszutek Wilk | 03e0edf | 2011-05-12 16:19:23 -0400 | [diff] [blame] | 565 | be->major = 0; |
| 566 | be->minor = 0; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 567 | xenbus_dev_fatal(dev, err, "creating vbd structure"); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | err = xenvbd_sysfs_addif(dev); |
| 572 | if (err) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 573 | xen_vbd_free(&be->blkif->vbd); |
Konrad Rzeszutek Wilk | 03e0edf | 2011-05-12 16:19:23 -0400 | [diff] [blame] | 574 | be->major = 0; |
| 575 | be->minor = 0; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 576 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | /* We're potentially connected now */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 581 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 586 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 587 | * Callback received when the frontend's state changes. |
| 588 | */ |
| 589 | static void frontend_changed(struct xenbus_device *dev, |
| 590 | enum xenbus_state frontend_state) |
| 591 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 592 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 593 | int err; |
| 594 | |
| 595 | DPRINTK("%s", xenbus_strstate(frontend_state)); |
| 596 | |
| 597 | switch (frontend_state) { |
| 598 | case XenbusStateInitialising: |
| 599 | if (dev->state == XenbusStateClosed) { |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 600 | pr_info(DRV_PFX "%s: prepare for reconnect\n", |
Konrad Rzeszutek Wilk | ebe8190 | 2011-05-12 16:42:31 -0400 | [diff] [blame] | 601 | dev->nodename); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 602 | xenbus_switch_state(dev, XenbusStateInitWait); |
| 603 | } |
| 604 | break; |
| 605 | |
| 606 | case XenbusStateInitialised: |
| 607 | case XenbusStateConnected: |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 608 | /* |
| 609 | * Ensure we connect even when two watches fire in |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 610 | * close succession and we miss the intermediate value |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 611 | * of frontend_state. |
| 612 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 613 | if (dev->state == XenbusStateConnected) |
| 614 | break; |
| 615 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 616 | /* |
| 617 | * Enforce precondition before potential leak point. |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 618 | * xen_blkif_disconnect() is idempotent. |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 619 | */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 620 | xen_blkif_disconnect(be->blkif); |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 621 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 622 | err = connect_ring(be); |
| 623 | if (err) |
| 624 | break; |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 625 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 626 | break; |
| 627 | |
| 628 | case XenbusStateClosing: |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 629 | xenbus_switch_state(dev, XenbusStateClosing); |
| 630 | break; |
| 631 | |
| 632 | case XenbusStateClosed: |
Joe Jin | 6f5986b | 2011-08-15 12:51:31 +0800 | [diff] [blame] | 633 | xen_blkif_disconnect(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 634 | xenbus_switch_state(dev, XenbusStateClosed); |
| 635 | if (xenbus_dev_is_online(dev)) |
| 636 | break; |
| 637 | /* fall through if not online */ |
| 638 | case XenbusStateUnknown: |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 639 | /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 640 | device_unregister(&dev->dev); |
| 641 | break; |
| 642 | |
| 643 | default: |
| 644 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", |
| 645 | frontend_state); |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /* ** Connection ** */ |
| 652 | |
| 653 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 654 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 655 | * Write the physical details regarding the block device to the store, and |
| 656 | * switch to Connected state. |
| 657 | */ |
| 658 | static void connect(struct backend_info *be) |
| 659 | { |
| 660 | struct xenbus_transaction xbt; |
| 661 | int err; |
| 662 | struct xenbus_device *dev = be->dev; |
| 663 | |
| 664 | DPRINTK("%s", dev->otherend); |
| 665 | |
| 666 | /* Supply the information about the device the frontend needs */ |
| 667 | again: |
| 668 | err = xenbus_transaction_start(&xbt); |
| 669 | if (err) { |
| 670 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 671 | return; |
| 672 | } |
| 673 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 674 | err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 675 | if (err) |
| 676 | goto abort; |
| 677 | |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 678 | err = xen_blkbk_discard(xbt, be); |
| 679 | |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 680 | /* If we can't advertise it is OK. */ |
| 681 | err = xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); |
| 682 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 683 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 684 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 685 | if (err) { |
| 686 | xenbus_dev_fatal(dev, err, "writing %s/sectors", |
| 687 | dev->nodename); |
| 688 | goto abort; |
| 689 | } |
| 690 | |
| 691 | /* FIXME: use a typename instead */ |
| 692 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 693 | be->blkif->vbd.type | |
| 694 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 695 | if (err) { |
| 696 | xenbus_dev_fatal(dev, err, "writing %s/info", |
| 697 | dev->nodename); |
| 698 | goto abort; |
| 699 | } |
| 700 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 701 | (unsigned long) |
| 702 | bdev_logical_block_size(be->blkif->vbd.bdev)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 703 | if (err) { |
| 704 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", |
| 705 | dev->nodename); |
| 706 | goto abort; |
| 707 | } |
| 708 | |
| 709 | err = xenbus_transaction_end(xbt, 0); |
| 710 | if (err == -EAGAIN) |
| 711 | goto again; |
| 712 | if (err) |
| 713 | xenbus_dev_fatal(dev, err, "ending transaction"); |
| 714 | |
| 715 | err = xenbus_switch_state(dev, XenbusStateConnected); |
| 716 | if (err) |
Joe Perches | 08b8bfc | 2011-06-12 09:21:13 -0700 | [diff] [blame] | 717 | xenbus_dev_fatal(dev, err, "%s: switching to Connected state", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 718 | dev->nodename); |
| 719 | |
| 720 | return; |
| 721 | abort: |
| 722 | xenbus_transaction_end(xbt, 1); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | static int connect_ring(struct backend_info *be) |
| 727 | { |
| 728 | struct xenbus_device *dev = be->dev; |
| 729 | unsigned long ring_ref; |
| 730 | unsigned int evtchn; |
| 731 | char protocol[64] = ""; |
| 732 | int err; |
| 733 | |
| 734 | DPRINTK("%s", dev->otherend); |
| 735 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 736 | err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", |
| 737 | &ring_ref, "event-channel", "%u", &evtchn, NULL); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 738 | if (err) { |
| 739 | xenbus_dev_fatal(dev, err, |
| 740 | "reading %s/ring-ref and event-channel", |
| 741 | dev->otherend); |
| 742 | return err; |
| 743 | } |
| 744 | |
| 745 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; |
| 746 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", |
| 747 | "%63s", protocol, NULL); |
| 748 | if (err) |
| 749 | strcpy(protocol, "unspecified, assuming native"); |
| 750 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) |
| 751 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; |
| 752 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) |
| 753 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; |
| 754 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) |
| 755 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; |
| 756 | else { |
| 757 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); |
| 758 | return -1; |
| 759 | } |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 760 | pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n", |
Konrad Rzeszutek Wilk | ebe8190 | 2011-05-12 16:42:31 -0400 | [diff] [blame] | 761 | ring_ref, evtchn, be->blkif->blk_protocol, protocol); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 762 | |
| 763 | /* Map the shared frame, irq etc. */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 764 | err = xen_blkif_map(be->blkif, ring_ref, evtchn); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 765 | if (err) { |
| 766 | xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", |
| 767 | ring_ref, evtchn); |
| 768 | return err; |
| 769 | } |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /* ** Driver Registration ** */ |
| 776 | |
| 777 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 778 | static const struct xenbus_device_id xen_blkbk_ids[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 779 | { "vbd" }, |
| 780 | { "" } |
| 781 | }; |
| 782 | |
| 783 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 784 | static DEFINE_XENBUS_DRIVER(xen_blkbk, , |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 785 | .probe = xen_blkbk_probe, |
| 786 | .remove = xen_blkbk_remove, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 787 | .otherend_changed = frontend_changed |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 788 | ); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 789 | |
| 790 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 791 | int xen_blkif_xenbus_init(void) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 792 | { |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 793 | return xenbus_register_backend(&xen_blkbk_driver); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 794 | } |