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